Closed
Description
Bug Report
π Search Terms
- record template literal types [infer]
- record template literal key
- template literal types
Found #29718 but not sure if it is the same error
π Version & Regression Information
TypeScript v4.2.0-dev.20201230
β― Playground Link
π» Code
// type Color = string // This works
type Color = `color${string}`
type Colors = Record<Color, string>;
const colors: Colors = {
'colorBlue': 'Blue',
// BUG. should prompt error?
'invalid': 'Red',
}
// BUG. color infers to `unkown` instead of `string`.
const color = getKeyValue(colors, 'colorBlue')
function getKeyValue<
T extends Record<string, unknown>,
K extends keyof T
>(obj: T, key: K): T[K] {
return obj[key];
}
π Actual behavior
getKeyValue(colors, 'colorBlue')
infersunknown
'invalid': 'Red',
is still valid key forcolors: Colors
π Expected behavior
getKeyValue(colors, 'colorBlue')
should inferstring
return value'invalid': 'Red',
should prompt an error as it does not match template literal type