Skip to content

Mapping with enum key type and literal value type gives not assignable error. #48217

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

Open
Troublor opened this issue Mar 11, 2022 · 1 comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@Troublor
Copy link

Troublor commented Mar 11, 2022

Bug Report

πŸ”Ž Search Terms

map enum key literal value

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about typing for maps.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

// We can quickly address your report if:
//  - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
//  - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.
//  - The incorrectness of the behavior is readily apparent from reading the sample.
// Reports are slower to investigate if:
//  - We have to pare too much extraneous code.
//  - We have to clone a large repo and validate that the problem isn't elsewhere.
//  - The sample is confusing or doesn't clearly demonstrate what's wrong.

export enum E {
    'a-b-c' = 'abc.com',
    'def' = 'def.com',
}

export type M = {
    [e in E]?: 'literal';
}

const m0: M = {
    [E['a-b-c']]: 'literal', // error here
    [E.def]: 'literal' // no error
};

export {m0};

πŸ™ Actual behavior

When use Enum['value'] as the key of a mapping, values assigned to string literal types are not correctly recognized.
The string literal is always lifted as string type and there is an error Type of computed property's value is 'string', which is not assignable to type '"literal"'.

However, if Enum.value form is used as the mapping key, everything works fine.

πŸ™‚ Expected behavior

The aforementioned error should not occur.
Type check should be consistent when using Enum['value'] and Enum.value.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Mar 11, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 11, 2022
@Zzzen
Copy link
Contributor

Zzzen commented Mar 14, 2022

Related to #29718

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants