-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Numeric literals assignable to enum literals only when values match #51561
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
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at dadfbcb. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at dadfbcb. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at dadfbcb. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at dadfbcb. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at dadfbcb. You can monitor the build here. Update: The results are in! |
@ahejlsberg Here are the results of running the user test suite comparing Everything looks good! |
@ahejlsberg Here they are:Comparison Report - main..51561
System
Hosts
Scenarios
Developer Information: |
@@ -8659,7 +8660,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||
|
|||
function formatUnionTypes(types: readonly Type[]): Type[] { | |||
const result: Type[] = []; | |||
let flags: TypeFlags = 0; | |||
let flags = 0 as 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.
Consider adding TypeFlags.Uninitialized = 0
?
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.
That would be an alternative. But I prefer 0 as TypeFlags
as an indicator this isn't actually a permitted value.
@ahejlsberg Here are the results of running the top-repos suite comparing Something interesting changed - please have a look. Details
|
@typescript-bot run dt |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 4220f58. You can monitor the build here. |
For purposes of representing collections of bit flags as enum types we have always permitted type
number
to be assigned to any numeric enum literal type (becausenumber
is the type that results when bit flags are or'd and and'ed using the|
and&
operators). However, we have also permitted any numeric literal type to be assigned to any numeric enum literal type, even when the underlying numeric values differ. This rule makes little sense and is revoked by this PR.The general effect of this change is that when a numeric type value is known not to match any of the declared members of an enum type (because the numeric value has a literal type that doesn't have a match in the enum type), the numeric type value is not assignable to that enum type. This in particular means that the literal value
0
cannot be used to initialize a bit flags enum type unless the enum type declares a member with the value0
.Note above that it is always possible to use an
as
assertion to coerce an arbitrary numeric value to an enum type.Fixes #51466.