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
interfaceFoo{asdf: string;hjkl: number;}letpartialFoo: Partial<Foo>;typeFnThatTakesPick=<KextendskeyofFoo>(x: Pick<Foo,K>)=>void;letfnThatTakesPick: FnThatTakesPick;fnThatTakesPick(partialFoo);// ^ Argument of type 'Partial<Foo>' is not assignable to parameter of type 'Pick<Foo, "asdf" | "hjkl">'.// Property 'asdf' is optional in type 'Partial<Foo>' but required in type 'Pick<Foo, "asdf" | "hjkl">'.// but fnThatTakesPick essentially takes a partial!// In fact, it accepts exactly the same objects:typeFnThatTakesPartial=(x: Partial<Foo>)=>void;letfnThatTakesPartial: FnThatTakesPartial;consta={};fnThatTakesPick(a);fnThatTakesPartial(a);constb={asdf: ''};fnThatTakesPick(b);fnThatTakesPartial(b);constc={hjkl: 0};fnThatTakesPick(c);fnThatTakesPartial(c);constd={asdf: '',hjkl: 0};fnThatTakesPick(d);fnThatTakesPartial(d);// also, note that the reverse works:const_=<KextendskeyofFoo>(x: Pick<Foo,K>)=>{fnThatTakesPartial(x);};
Never mind they're not the same, apparently optional properties effectively add | undefined to the type of a key:
constb={asdf: undefined};fnThatTakesPick(b);// errors if you turn on strictNullChecksfnThatTakesPartial(b);// no errorconstc={hjkl: undefined};fnThatTakesPick(c);// errors if you turn on strictNullChecksfnThatTakesPartial(c);// no errorconstd={asdf: undefined,hjkl: undefined};fnThatTakesPick(d);// errors if you turn on strictNullChecksfnThatTakesPartial(d);// no error
Playground link (you have to manually turn on strictNullChecks)
TypeScript Version: 2.8.1
Search Terms: pick, partial, optional
Code
Expected behavior:
No typechecking errors.
Actual behavior:
The marked typechecking errors.
Playground Link
Related Issues:
Pick
not propagating optionality andreadonly
-nessPick
treats computed propertiesThe text was updated successfully, but these errors were encountered: