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
Code
It is a common situation that provides some default params to required options and make them be optional to bring convenient calling usage.
interfaceHasHeight{height: number}typeOmit<P,KextendskeyofP>=Pick<P,Exclude<keyofP,K>>functionmakeHeightOptional<PextendsHasHeight>(heightRequired: (p: P)=>number){returnfunctionheightOptional(params: Omit<P,'height'>&{height?: number}){constheight=params.height||10constnewParams={ ...params, height }// Argument of type 'Pick<P, Exclude<keyof P, "height">> & { height: number; }' is not assignable to parameter of type 'P'.returnheightRequired(newParams)}}
Expected behavior:
code works
Actual behavior:
TS report:
Argument of type 'Pick<P, Exclude<keyof P, "height">> & { height: number; }' is not assignable to parameter of type 'P'.
TypeScript Version: 3.2.2
Search Terms:
Code
It is a common situation that provides some default params to required options and make them be optional to bring convenient calling usage.
Expected behavior:
code works
Actual behavior:
TS report:
Playground Link: http://www.typescriptlang.org/play/index.html#src=interface%20HasHeight%20%7B%20%0A%20%20height%3A%20number%0A%7D%0A%0Atype%20Omit%3CP%2C%20K%20extends%20keyof%20P%3E%20%3D%20Pick%3CP%2C%20Exclude%3Ckeyof%20P%2C%20K%3E%3E%0A%0Afunction%20makeHeightOptional%3CP%20extends%20HasHeight%3E(heightRequired%3A%20(p%3A%20P)%20%3D%3E%20number)%20%7B%0A%20%20return%20function%20heightOptional(params%3A%20Omit%3CP%2C%20'height'%3E%20%26%20%7B%20height%3F%3A%20number%20%7D)%20%7B%0A%20%20%20%20const%20height%20%3D%20params.height%20%7C%7C%2010%0A%20%20%20%20const%20newParams%20%3D%20%7B%20...params%2C%20height%20%7D%0A%0A%20%20%20%20return%20heightRequired(newParams)%0A%20%20%7D%0A%7D%0A%0A
Related Issues:
The text was updated successfully, but these errors were encountered: