You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the correct behavior. If you imagine that we inferred obj's return type to be Record<T, number> then this code would be allowed, but it's unsound:
I thought this was incorrect behavior because the specialized version of the function has the expected type {foo: number}, but now I realize that in order to accomplish that you'd need the return type of obj to be something like T is literal ? Record<T, number> : { [x: string]: number; }, and is literal does not exist in TypeScript. Sorry for the erroneous report, and thank you for the explanation!
Return type for obj<"foo" | "bar">("foo") should be { foo?: number, bar?: number } but for obj<"foo">("foo") it can be { foo: number }
Even if you do widen the domain of keys to all strings, inferred type should be { [x: string]?: number; } because not every string key will be defined. It is still unsound.
TypeScript Version: 4.2.0-dev.20201202
Search Terms: computed property
Code
Expected behavior: this should compile without error.
Actual behavior:
error TS2741: Property 'foo' is missing in type '{ [x: string]: number; }' but required in type '{ foo: number; }'.
Playground Link: https://www.typescriptlang.org/play?ts=4.2.0-dev.20201202#code/GYVwdgxgLglg9mABHARgKwDwBVEFMAeUuYAJgM6JlQBOMYA5gHwAUA1rgJ4BciWAlIgDeAKESJquKCGpJBiANrsOAXR4AGRAF8A3MM3CICKuNxkQAGyg85wOHB5gQAWxS5qWxAF5k6DACJbOD8WALs-Pl0gA
Related Issues:
keyof
and computed property keysThe text was updated successfully, but these errors were encountered: