🔎 Search Terms
generic function, partial, optional, mapped type, indexed access, undefined, bug, regression
🕗 Version & Regression Information
- This is a bug
- This changed between versions 4.5.5 and 4.6.2
- This is the behavior in every version I tried (all playground versions)
⏯ Playground Link
https://www.typescriptlang.org/play?target=99&jsx=0&pretty=true&ts=4.6.2#code/C4TwDgpgBA8gRgKygXigbwFBW1AhgLigEYAaLHOQgJgwF8BuDDAYwHsA7AZ2CgFtcwkACaFMOKAG0A0lACW7KAGsIIVgDMoABVwAnYLNwAbADzwEAPgC6hM9Mt0U6WkzZceOiJ1aGAbhACyAsKOxjIQAB7AEOxCnEoq6lCgkIn8ghBC5gAUyiCEUgCUKOZ8QRkSuZYAdMCsAMrAOvIA5lkFjAD0HVAAegC0fVAAojo6rDpQEZDMUUJQWQAGacIVKpYLcnFgrJycsnCGIFAArjEQavIZBRhAA
💻 Code
type Obj = {
a: 1,
b: 2
};
const mapped: {
[K in keyof Partial<Obj>]: Obj[K]
} = {}
const resolveMapped = <K extends keyof typeof mapped>(key: K) => mapped[key].toString();
// ^-- Error expected (`mapped[key]` is possibly undefined)
🙁 Actual behavior
TypeScript does not recognize mapper[key] might be undefined and emits broken code without errors
🙂 Expected behavior
Correct type inference from pre-v4.6.x
Additional information about the issue
No response