-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request to expose zeroType
emptyStringType
and isTypeAssignableTo
on the TS TypeChecker
#50694
Comments
It seems like TypeScript/src/compiler/types.ts Lines 4718 to 4735 in 7737473
@DanielRosenwasser what do you think? |
Oooo! I think exposing |
Curious if any more thought has been given to this? Just today, something came up for me again where having this would have definitely made my life easier. |
Suggestion
Proposal to expose
zeroType
(asgetZeroType()
),emptyStringType
(asgetEmptyStringType()
) andisTypeAssignableTo
on the TS TypeChecker🔍 Search Terms
#zeroType, #emptyStringType, #isTypeAssignableTo
✅ Viability Checklist
Searched issues for: zeroType, emptyStringType, isTypeAssignableTo
There are two open issues discussing the request to expose
isTypeAssignableTo
, but it appears to have fizzled out. Links here:#11728 and #9879
My suggestion meets these guidelines:
⭐ Suggestion
The TS TypeChecker already exposes
getNullType()
,getFalseType()
, andgetUndefinedType()
. I'm using all of these in an ESLint rule I'm working on. However, I have no trivial way to get at the0
type or the''
(empty string) type. TheThe
zeroType
and theemptyStringType
do indeed appear to exist in the TS code (checker.ts lines 1011 and 1012):But they do not appear to be exposed like
getNullType()
,getUndefinedType()
, andgetFalseType()
.Presently, I have a dirty dirty hack where I create a program in memory and yank out the types, as in:
Besides being pretty ugly, probably inefficient and just generally offending my sensibilities, this has some very practical drawbacks. For instance, any
0
type retrieved viagetTypeAtLocation()
from the actual code being linted, will not be equal to the poorly generatedzeroType
I create in my dirty function above (same foremptyStringType
). In fact, even callingisTypeAssignableTo(zeroType, realZeroTypeFromLintedCode)
will return false! I suspect there may be other cases as well, where is it unsafe/unreliable to use these generated types and expect them to behave in a reliable way within the program actually being linted.📃 Motivating Example
The main motivation is to support an effort to create a new ESLint rule. Proposal for this rule is available here: typescript-eslint/typescript-eslint#5592
There you will find a (collapsed) list of ~50ish test cases that illustrate what the rule should do. These test cases are likely the most insightful bit of code in terms of understanding the spirit of the rule.
💻 Use Cases
As the ESLint rule needs to identify cases where a type is assignable one (and only one) falsy value, exposing
getZeroType()
andgetEmptyStringType()
would eliminate the dirty code I posted above. Here is an snippet from the rule's code of how the type would be used:In the above snippet,
leftSideFalsies[0] === rightType
does not always work. When rightType (which actually retrieved from the code being linted) is compared against a generatedzeroType
oremptyStringType
, this check fails. To work around this, the actual code being used today is:This appears to work (for my needs) but it gives me a similarly yucky feeling to the dirty function that generates the zeroType and the emptyStringType (which is the whole reason this hack is needed in the first place).
The text was updated successfully, but these errors were encountered: