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

Incorrect type inference of deep nested types #43009

Closed
captain-yossarian opened this issue Mar 1, 2021 · 4 comments
Closed

Incorrect type inference of deep nested types #43009

captain-yossarian opened this issue Mar 1, 2021 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@captain-yossarian
Copy link

captain-yossarian commented Mar 1, 2021

Bug Report

πŸ”Ž Search Terms

SO question

⏯ Playground Link

Playground

πŸ’» Code

type ZERO = 0
type Next<T> = T extends ZERO ? { v: ZERO } : (T extends { v: infer U } ? { v: Next<U> } : never)

type ONE = Next<ZERO>
type TWO = Next<ONE>
type THREE = Next<TWO>
type FOUR = Next<THREE>
type FIVE = Next<FOUR>
type SIX = Next<FIVE>
type SEVEN = Next<SIX>
type EIGHT = Next<SEVEN>
type NINE = Next<EIGHT>
type TEN = Next<NINE>

type TEq<T, P> = T extends P ? (P extends T ? true : never) : never

let v1: TEq<TWO, TWO> = true // correct: types is equal
let v3: TEq<SIX, SEVEN> = true // error: why type of v3 inferred as true?

type Result = TEq<SIX, SEVEN>

type Result_the_same_type = {
    v: {
        v: {
            v: {
                v: {
                    v: {
                        v: ZERO;
                    };
                };
            };
        };
    };
} extends {
    v: {
        v: {
            v: {
                v: {
                    v: {
                        v: {
                            v: ZERO
                        };
                    };
                };
            };
        };
    };
} ? true : false // false

/**
 * Why Result is true
 * and Result_the_same_type is false
 */

As you see Result and Result_the_same_type are structuraly equal.

πŸ™ Actual behavior

Result is true
Result_the_same_type is false

πŸ™‚ Expected behavior

Result should be false

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 2, 2021
@RyanCavanaugh
Copy link
Member

See #35533 (comment)

@DarkGenius
Copy link

See #35533 (comment)

Is there some workaround for this case?

@captain-yossarian
Copy link
Author

@DarkGenius what about:

type AllowedLevels = 1 | 2 | 3 | 4 | 5;

type Levels<T, Cache extends Array<any> = []> = {

  [P in keyof T]: T[P] extends object ? Levels<T[P], [...Cache, 1]> : Cache

}[keyof T];

type Allowed<T> = Levels<T> extends { length: AllowedLevels } ? true : false;

type Result=Allowed<SIX> // true
type Result2=Allowed<SEVEN> // false

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants