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

String template literals as object keys don't get validated correctly #58673

Closed
wbolduc opened this issue May 27, 2024 · 5 comments
Closed

String template literals as object keys don't get validated correctly #58673

wbolduc opened this issue May 27, 2024 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@wbolduc
Copy link

wbolduc commented May 27, 2024

πŸ”Ž Search Terms

String template literal object keys return type function

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and there is nothing in the FAQ about it

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.4.5#code/MYewdgzgLgBBCGBbADgGwKYEYYF4YAoAPALjAFdEAjdAJwEpiYAldUGgEwB4ADBFDAPoASAN7kqtAL7cANNBoBLMAHMAfLnX4RAKBh6YAbV5I06YSMLSAuowBEACXSpUIWzO2S627aEiw+pgBMuAQk4tT0jCxsXMb8ZqLhUrLySmoaBDr6MAD0OTBKbDSsUKgAnjAAbvCoCuwwANboZbr6RgBC8PW1ULQ1MKKW3DYwtpRdMOPsbh5ePuDQcCYYAMwhRKQUEQzMrCAcPAGCiVvJclCKKuo4mln6eTBFJeUF7OhgUAoAZgro9VONZqtPS2ZTwGjjZToWx2YoxWyzIA

πŸ’» Code

const sample1 = (x:number): Record<`sample_${number}`,string> => ({
    [`sample_${x}`]: "Hello",
})

const sample2 = (x:number): Record<`sample_${number}`,string> => ({
    // incorrectly valid key
    [`Bad literal ${x}`]: "bad bad",
})

const sample3 = (x:number): Record<`sample_${number}`,string> => ({
    // correctly identified bad key
    "garbage": "record"
})

πŸ™ Actual behavior

Typescript does not error on incorrect string template literals

πŸ™‚ Expected behavior

Typescript should show sample2 as having a bad object key like it does in sample3

Additional information about the issue

Bonus feature request/question: Is this the only way to get this sort of return type from a function. Putting "as const" in the key or after the object doesn't seem to correctly infer the type. It stays wide {[x:string]: string}

@jcalz
Copy link
Contributor

jcalz commented May 27, 2024

Duplicate of #13948.

(#13948 (comment))

@fatcerberus
Copy link

fatcerberus commented May 28, 2024

Note that Record<`sample_${number}`, string> doesn’t restrict the type to only those keys, it just guarantees that any properties with keys matching the pattern are typed as string.

@wbolduc
Copy link
Author

wbolduc commented May 28, 2024

I mean I guess strictly from the perspective of something receiving the type but TS still checks that you're putting in the correct things when you're initially creating the object

const aa: Record<number, boolean> = {
	[2]: true,
	salami: "sandwich",
};

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAhnAXDASgU1AJwCYB4wCuAtgEZqYA0MJIIANmnGAHwwC8MA3gFACQA2gCYAusiiYCaCnwhw6cIgEtkAIllhsAd0XAAFiukBfANzcgA

@fatcerberus
Copy link

Only when assigning the object literal to the type directly:

const bb = {
	[2]: true,
	salami: "sandwich",
};
const aa: Record<number, boolean> = bb;  // ok, no error

The excess property check isn't comprehensive, you shouldn't rely on it to constrain the allowed values of a type. That's why issue #12936 exists.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 13, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 16, 2024
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