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
interfacePropMap{a: {b: string|number;};c: never|undefined;}functionfunc<KextendskeyofPropMap,PextendsPropMap[K]>(k: K,p: P){}functionfunc2<KextendskeyofPropMap,PextendsPropMap[K]>(k: K,p?: P){}/* ✅ */func('a');// should error, needs prop./* ✅ */func('a',{b: ''});// should be fine/* ✅ */func('a',{c: ''});// should error, wrong prop./* ❌ */func('c');// should be fine, I don't want to pass in props for never|undefined./* ✅ */func('c',undefined);// should be fine/* ✅ */func('c',{c: ''});// should error, should be given no prop /* ❌ */func2('a');// should error, needs prop./* ✅ */func2('a',{b: ''});// should be fine/* ✅ */func2('a',{c: ''});// should error, wrong prop./* ✅ */func2('c');// should be fine, I don't want to pass in props for never|undefined./* ✅ */func2('c',undefined);// should be fine/* ✅ */func2('c',{c: ''});// should error, should be given no prop
I hope the above makes sense, but I can't seem to make this act nicely. There is a use case where it breaks.
(I would love to split it into 2 functions, but sadly, it's not my call.)
With func the func('c') breaks because it is saying I NEED to supply a property
so I thought, simple fix.. make p optional.
but as you can see from func2 this makes func('a') not throw an error, when I WANT it to, because it has a P that is not never.
Playground
I hope the above makes sense, but I can't seem to make this act nicely. There is a use case where it breaks.
(I would love to split it into 2 functions, but sadly, it's not my call.)
With
func
thefunc('c')
breaks because it is saying I NEED to supply a propertyso I thought, simple fix.. make p optional.
but as you can see from
func2
this makesfunc('a')
not throw an error, when I WANT it to, because it has a P that is not never.other things I tried was
which means I can't pass 'undefined' as the 2nd parameter, which is perfectly fine, I can live with that..
The text was updated successfully, but these errors were encountered: