Skip to content

Template literals as keys in object literals loosing type information #41912

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

Closed
falsyvalues opened this issue Dec 10, 2020 · 7 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@falsyvalues
Copy link

Template literals as keys in object literals loosing type information, even with explicite type literals (works only with one exact type).

TypeScript Version: 4.1.2, 4.2.0-dev.20201210

Search Terms: Template literal types literal keys object keys literal types

Code

const template = <T extends string>(name: T) => ({
    [`${name}` as const]: name
});

Expected behavior:

const value = template ('TEST'); // const value: { TEST: "TEST"; }

Actual behavior:

const value = template ('TEST'); // const value: { [x: string]: "TEST"; }

Playground Link: https://www.typescriptlang.org/play?ts=4.2.0-dev.20201210&ssl=13&ssc=26&pln=13&pc=44#code/MYewdgzgLgBFCmBbADgGwIYIPphFLA7iAE4DWAlmAOZboRanwCeMAvDADwAqM8AHgjAATCDGjFKVAHwAKMOkTwAXDC4BKNlJgyA3gCgYhmAG0ABgBId8xQF9TMOjFCQoAXRXX4em2oDcevWdoGAA3dFQAV3gcPEISCmo2OCQ0TGjcfCIySVp6RiYZAHIuAFEAZS5CvxgAehqncGCwyOUYHRM+FXFJdxgAIlKKvt8YGwCg2AQUDGwMuOzqQnIoAAssVGX4YnCsKCZkeCTuXgF4YVFu6llPFWLyypgAHxhCgHkAJUKnl4A5V8qNKwtLoDEYzJZPHYHKIJr1PN4-ONGrBmlEYpl4jkNghtqgklNUrNYlkEjQCMs1titjs9gcioMASM6g0XKFwlEVO1jJ0xFAJNRegN7sNRkjWQSZtESZIjjx+IIRLz+dI5ApWncKlVNNp9EYTBYrGqoY5YR41Qj-IFkWyWvNSfiUpK7ZJ6fcqkz6hMbRy2qp7iohUMRmMgA

Related Issues: Might be related to #41631

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 10, 2020
@RyanCavanaugh
Copy link
Member

This is correct behavior, since you could write template<"foo" | "bar">("foo") and would not get back an object with a bar property

@falsyvalues
Copy link
Author

This is correct behavior, since you could write template<"foo" | "bar">("foo") and would not get back an object with a bar property

@RyanCavanaugh But this will not produce expected result either I won't get // const value: { foo: "foo"; } even if value is there.

@RyanCavanaugh
Copy link
Member

I don't understand what you mean by that.

@typescript-bot
Copy link
Collaborator

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

@falsyvalues
Copy link
Author

Hi @dishuostec!

Thanks for that. Looks returning type and as any did the job but would be nice achieve that without casting to any. @RyanCavanaugh Please take a look on example above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants