Skip to content
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

Can not infer not null / undefined when use enum as the optional key #30714

Closed
taoalpha opened this issue Apr 2, 2019 · 3 comments
Closed

Can not infer not null / undefined when use enum as the optional key #30714

taoalpha opened this issue Apr 2, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@taoalpha
Copy link

taoalpha commented Apr 2, 2019

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:
enum as key
Code

enum A {
  a,
  b,
  c
}

type b = A.a|A.b

const a: {
  [key in A]?: {
    [key in A]?: string
  }
} = {
  [A.a]: {
    [A.a]: "a"
  }
}

function t() {
  // below line will have error on a[A.a][A.a] for possibly `undefined` when enabled `strictNullChecks`
  if (a[A.a] && a[A.a][A.a]) {
    return a[A.a][A.a];
    }
    return
}

Expected behavior:
no err when enable strictNullChecks, as the undefined should be ruled out after first a[A.a] check.

Actual behavior:

Object is possibly 'undefined'. for a[A.a][A.a]

Playground Link: https://www.typescriptlang.org/play/#src=enum%20A%20%7B%0D%0A%20%20a%2C%0D%0A%20%20b%2C%0D%0A%20%20c%0D%0A%7D%0D%0A%0D%0Atype%20b%20%3D%20A.a%7CA.b%0D%0A%0D%0Aconst%20a%3A%20%7B%0D%0A%20%20%5Bkey%20in%20A%5D%3F%3A%20%7B%0D%0A%20%20%20%20%5Bkey%20in%20A%5D%3F%3A%20string%0D%0A%20%20%7D%0D%0A%7D%20%3D%20%7B%0D%0A%20%20%5BA.a%5D%3A%20%7B%0D%0A%20%20%20%20%5BA.a%5D%3A%20%22a%22%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Afunction%20t()%20%7B%0D%0A%20%20if%20(a%5BA.a%5D%20%26%26%20a%5BA.a%5D%5BA.a%5D)%20%7B%0D%0A%20%20%20%20return%20a%5BA.a%5D%5BA.a%5D%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20return%0D%0A%7D

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 2, 2019
@RyanCavanaugh
Copy link
Member

Duplicate #28081

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@jcalz
Copy link
Contributor

jcalz commented Dec 23, 2019

The fix for #28081, namely #31478, did not address enum keys; should this be reopened or should a new issue be filed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants