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
There's some odd behaviour with mapped types on generic types with extends constraints.
TypeScript Version: 2.9.0-dev.20180330
Search Terms:
generic constraint
Code
typeEntity={id: string;};functionf<TextendsEntity>(){constwhere: {[KinkeyofT]?: T[K]}={id: "asdf"};// this throws type errorwhere.id="asdf";// this is valid}
Expected behavior:
No type error
Actual behavior:
Getting type error on first assignment: Type '{ id: "asdf"; }' is not assignable to type '{ [K in keyof T]?: T[K]; }'.
Playground Link:
Related Issues:
None that I know of
The above code snippet throws a type error even though it is safe. Presumably, because only a subset of keys of T are known at this point, the compiler can't check if the given object literal contains all keys of T. However, the type is partial and skipping the unknown keys is perfectly safe. It would be nice if this could be detected and allowed or at least a better error be produced. I spent way too much time trying to figure out why this didn't work.
The text was updated successfully, but these errors were encountered:
dhruvrajvanshi
changed the title
Partial mapped types on constrained generics should allow object literals to specify known keys
Partial mapped types on constrained generics should allow object literals to specify only known keys
Mar 30, 2018
There's some odd behaviour with mapped types on generic types with extends constraints.
TypeScript Version: 2.9.0-dev.20180330
Search Terms:
generic constraint
Code
Expected behavior:
No type error
Actual behavior:
Getting type error on first assignment: Type '{ id: "asdf"; }' is not assignable to type '{ [K in keyof T]?: T[K]; }'.
Playground Link:
Related Issues:
None that I know of
The above code snippet throws a type error even though it is safe. Presumably, because only a subset of keys of T are known at this point, the compiler can't check if the given object literal contains all keys of T. However, the type is partial and skipping the unknown keys is perfectly safe. It would be nice if this could be detected and allowed or at least a better error be produced. I spent way too much time trying to figure out why this didn't work.
The text was updated successfully, but these errors were encountered: