-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Remove redundant primitive types from intersections #23751
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a straightforward refactor of TypeIncludes -> TypeFlags plus the small change to simplify intersections. One comment about a typo on the test, though, which may impact that strictNullChecks
case.
@@ -0,0 +1,15 @@ | |||
// @strict |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// @strict: true
This isn't setting any compiler options as is and becomes just a comment in the output. Though I guess it's heartening to know that nothing in this change actually relies on strict
. 😛
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. We have hundreds of tests that include // @strict: true
and they definitely behave differently if you remove the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
....this comment isn't // @strict: true
, it's just // @strict
. This comment does nothing.
src/compiler/checker.ts
Outdated
for (const type of types) { | ||
includes = addTypeToIntersection(typeSet, includes, getRegularTypeOfLiteralType(type)); | ||
} | ||
return includes; | ||
} | ||
|
||
function removeRedundantPrimtiveTypes(types: Type[], includes: TypeFlags) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: should be removeRedundantPrimitiveTypes
This PR removes primitive types
string
,number
, andsymbol
from intersections that also contain literal types from the same domain. For examplestring & 'a'
is reduced to just'a'
. Likewise,10 | number
is reduced to just10
.The PR eliminates the
TypeIncludes
enum from the type checker in favor of just usingTypeFlags
. This allows us to more efficiently compute the combined flags during the construction of a union or intersection type.