Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking generic conditional type distribution prevents recursion #32290

Closed
arielshaqed opened this issue Jul 8, 2019 · 3 comments
Closed

Breaking generic conditional type distribution prevents recursion #32290

arielshaqed opened this issue Jul 8, 2019 · 3 comments

Comments

@arielshaqed
Copy link

TypeScript Version: Current playground, Version 3.6.0-dev.20190704.

Search Terms: conditional type

Code

type YN<T> = [T] extends [Record<string, any>] ? {
    [K in keyof T]: YN<T[K]>;
} : 'x';

type C = YN<number | undefined>;            // 'x'
type D = YN<{ x: number; y?: number; }>;    // expected { x: 'x', y: 'x'}

const d: D = { x: 'x', y: 'x' };

Expected behavior:

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.

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!

@dragomirtitian
Copy link
Contributor

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:

type YN<T> = [T] extends [Record<string, unknown>] ? {
    [K in keyof T]: YN<T[K]>;
} : 'x';

type C = YN<number | undefined>;            // 'x'
type D = YN<{ x: number; y?: number; }>;    // expected { x: 'x', y: 'x'}

const d: D = { x: 'x', y: 'x' };

playground

As the saying goes: any is weird, use unknown 😉

@arielshaqed
Copy link
Author

Thanks!

unknown seems to fix the issue. There is still weirdness in the type we get. (I'd probably understand getting an error message, though.)

@arielshaqed
Copy link
Author

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants