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
D should be the type { x: 'x', y: 'x' }, and the code should compile.
Actual behavior:
It doesn't compile,
../../../../../../tmp/gen-dist.ts:8:16 - error TS2322: Type 'string' is not assignable to type 'number'.
8 const d: D = { x: 'x', y: 'x' };
~
../../../../../../tmp/gen-dist.ts:6:15
6 type D = YN<{ x: number; y?: number; }>; // expected { x: 'x', y: 'x'}
~
The expected type comes from property 'x' which is declared here on type '{ x: number; y?: number; }'
../../../../../../tmp/gen-dist.ts:8:24 - error TS2322: Type 'string' is not assignable to type 'number'.
8 const d: D = { x: 'x', y: 'x' };
~
../../../../../../tmp/gen-dist.ts:6:26
6 type D = YN<{ x: number; y?: number; }>; // expected { x: 'x', y: 'x'}
~
The expected type comes from property 'y' which is declared here on type '{ x: number; y?: number; }'
Found 2 errors.
It looks as though the expansion of YN into the record fields did not work. Hovering over YN on line 6 in the playground link shows weird infinite-ish recursion.
I don't think this is necessarily the expected behavior but as far as I can tell the any in the Record is causing the problem. Since [T] extends [Record<string, any>] the compiler believes T[K] to be any so it simplifies the recursive application as YN<any> which will always take the true branch of the conditional (since any extends anything). Since mapped types applied to primitive type just keep the primitives you get back to T[K] when the type is actually applied.
As a workaround, using unknown instead of any behaves as you expect it to:
TypeScript Version: Current playground,
Version 3.6.0-dev.20190704
.Search Terms: conditional type
Code
Expected behavior:
D
should be the type{ x: 'x', y: 'x' }
, and the code should compile.Actual behavior:
It doesn't compile,
It looks as though the expansion of
YN
into the record fields did not work. Hovering overYN
on line 6 in the playground link shows weird infinite-ish recursion.Playground Link: https://www.typescriptlang.org/play/#code/C4TwDgpgBAmgcgHgCoD4oF4oG0kF0oQAewEAdgCYDO2AShAMYD2ATuQpcMwJakDmANFACGpECnwB+KAG8AUFAXYA0lB5QA1hBCMAZlDwAuWIiRYluFAG5ZAXyhGA5IQfXZoSFADCGYwlIBXAFsAIwhmKAAfKH8KCB0eCHIrRRTUgHo0qCcHN3BoABEfeARpKEIjAJCwyygQCQqg0OYam2TFDIJCSHoSchkyx2dBEEGHG1lZJlIOKHIjQsxS8qyh2tGoG2sgA
Related Issues:
#30572
#30020
Bonus words:
This is a minimal failing example of our real code.
We are using the technique of #31751 (comment) to prevent distributing a conditional type over a union.
Thanks!
The text was updated successfully, but these errors were encountered: