-
Notifications
You must be signed in to change notification settings - Fork 12.9k
fix(47508): noUncheckedIndexedAccess with enums Type narrowed #49912
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
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
|
It looks like this also needs to check that the indexing enum type is the same as the indexed enum type, in other words this example should not have its behavior changed: enum A {
a, b, c
}
enum B {
x, y, z
}
const p = A[B.x]; |
|
||
const t = "testing" | ||
const value2 = Meat[t] | ||
|
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.
const t = "testing" | ||
const value2 = Meat[t] |
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.
t
variable name should be self-explaining
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.
Made some changes, please have a look!
return accessFlags & AccessFlags.IncludeUndefined ? getUnionType([indexInfo.type, undefinedType]) : indexInfo.type; | ||
if (accessFlags & AccessFlags.IncludeUndefined) { | ||
if (objectType.symbol && objectType.symbol.flags & (SymbolFlags.RegularEnum | SymbolFlags.ConstEnum) && (indexType.symbol && indexType.flags & TypeFlags.EnumLiteral && getParentOfSymbol(indexType.symbol) === objectType.symbol)) { | ||
return indexInfo.type; |
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 is still duplicating the line that we started with.
Proposed code (I haven't tested this):
// When accessing an enum object with its own type,
// e.g. E[E.A] for enum E { A }, undefined shouldn't
// be included in the result type
if ((accessFlags & AccessFlags.IncludeUndefined) &&
!(objectType.symbol &&
objectType.symbol.flags & (SymbolFlags.RegularEnum | SymbolFlags.ConstEnum) &&
(indexType.symbol &&
indexType.flags & TypeFlags.EnumLiteral &&
getParentOfSymbol(indexType.symbol) === objectType.symbol))) {
return getUnionType([indexInfo.type, undefinedType]);
}
return indexInfo.type;
Fixes #47508