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

Templated string literal type as key looses type when using non enumerable types like ${number} or ${string} in template #42796

Closed
rpedretti opened this issue Feb 14, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@rpedretti
Copy link

Bug Report

πŸ”Ž Search Terms

template literal string type
indexed array type

πŸ•— Version & Regression Information

  • Versions 4.1+

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type SubType = 'a' | 'b'
type TemplateType = Record<`sub_[${SubType}]`, number>
/*
type TemplateType = {
    "sub_[a]": number;
    "sub_[b]": number;
}
*/

type OtherType = Record<`other_[${boolean}]`, number>
/*
type OtherType = {
    "other_[false]": number;
    "other_[true]": number;
}
*/

type OwnsType = Record<`owns[${number}]`, number>
// OwnsType = {}

type Change<T> = <F extends keyof T>(prop: F, value: T[F]) => void;

const change: Change<TemplateType & OwnsType & OtherType> = (prop, value) => console.debug(prop, value);

change("owns[0]", '');
change("other_[false]", '');
change("sub_[a]", '');

πŸ™ Actual behavior

Having a templated string literal type looses type when using non enumerable types like ${number} or ${string} in template

πŸ™‚ Expected behavior

Templated literal string should resolve to actual type

I don't know if this is intended since it's not possible to enumerate keys for these kind of template but it would be a nice feature

@rpedretti rpedretti changed the title Templated string literal type as key looses type when using non enumerable types like ${number} or ${string} in template Templated string literal type as key looses type when using non enumerable types like ${number} or ${string} in template Feb 14, 2021
@jcalz
Copy link
Contributor

jcalz commented Feb 14, 2021

Duplicate #42192

@yogeshpanchal8195
Copy link

I can resolve this issue at a faster pace & with a better approch

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 18, 2021
@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.

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

5 participants