Skip to content

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

Merged
merged 5 commits into from
Sep 26, 2022

Conversation

navya9singh
Copy link
Member

@navya9singh navya9singh commented Jul 14, 2022

Fixes #47508

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jul 14, 2022
@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@navya9singh
Copy link
Member Author

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

#47508

@RyanCavanaugh
Copy link
Member

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];

@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jul 14, 2022

const t = "testing"
const value2 = Meat[t]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines 20 to 21
const t = "testing"
const value2 = Meat[t]

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

Copy link
Member Author

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;
Copy link
Member

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;

@navya9singh navya9singh merged commit bc9cbbe into main Sep 26, 2022
@navya9singh navya9singh deleted the fix/47508 branch September 26, 2022 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

noUncheckedIndexedAccess with enums could be type narrowed
5 participants