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
Actual behavior:Property 'dog' is missing in type '{ cat: number; }'.
I understand this might be working as intended insofar that, in the last invocation of doTest, it's unclear what generic T to infer for the function. If T is 'cat'|'dog', then Pick<Test, 'cat'|'dog'> would require both cat and dog, which the union type for catOrDog does not match.
On the other hand, it seems very odd for a function to accept both an argument of type { cat: number } and type { dog: number } but not type { cat: number }|{ dog: number }.
As a work-around, it is possible to type doTest with additional generics, like so:
declarefunctiondoTest<TextendskeyofCatAndDog,VextendskeyofCatAndDog>(x: Pick<CatAndDog,T>|Pick<CatAndDog,V>): void;doTest<"cat","dog">(catOrDog);// No eror
But this feels like a less than ideal solution.
The text was updated successfully, but these errors were encountered:
I feel like I've seen this sort of thing crop up for different people and I wonder if there's a known issue that tracks the general case of reduction of unions/intersections of overloaded/generic functions.
You want TypeScript to notice that doTest is the intersection of a function from Pick<Test,'cat'> to void and a function from Pick<Test,'dog'> to void, and treat it as a function from Pick<Test,'cat'> | Pick<Test,'dog'> to void. You can kind of force TypeScript to notice this without touching your declaration of doTest declaration, in the following ugly way:
Similarly, there's the flip side where one wants TypeScript to notice that you have a function which is the union of a function from A1 to R1 and a function from A2 to R2, and you want to be able to give it a parameter of type A1 & A2 and get a value of type R1 | R2:
It's probably a difficult problem to solve properly and lots of creeping unsoundness happens. But I wonder if it's tracked anywhere. See #16644 and #16716 for recent similar examples.
TypeScript Version: 2.3.4
Code
Expected behavior: Last line should not error
Actual behavior:
Property 'dog' is missing in type '{ cat: number; }'.
I understand this might be working as intended insofar that, in the last invocation of
doTest
, it's unclear what genericT
to infer for the function. IfT
is'cat'|'dog'
, thenPick<Test, 'cat'|'dog'>
would require bothcat
anddog
, which the union type forcatOrDog
does not match.On the other hand, it seems very odd for a function to accept both an argument of type
{ cat: number }
and type{ dog: number }
but not type{ cat: number }|{ dog: number }
.As a work-around, it is possible to type
doTest
with additional generics, like so:But this feels like a less than ideal solution.
The text was updated successfully, but these errors were encountered: