Skip to content

Stack overflow when inferring recursive type #37475

@plol

Description

@plol

TypeScript Version: 3.9.0-dev.20200319

Search Terms:
call stack exceeded

Code

export type A = "number" | "null" | A[];

export type F<T> = null extends T
    ? [F<NonNullable<T>>, "null"]
    : T extends number
    ? "number"
    : never;

export type G<T> = { [k in keyof T]: F<T[k]> };

interface K {
    b: number | null;
}

const gK: { [key in keyof K]: A } = { b: ["number", "null"] };

function foo<T>(g: G<T>): T {
    return {} as any;
}

foo(gK);

Expected behavior:
Not crashing the compiler

Actual behavior:

% ./node_modules/.bin/tsc --version
Version 3.9.0-dev.20200319
% ./node_modules/.bin/tsc bug.ts
...node_modules/typescript/lib/tsc.js:81982
                throw e;
                ^

RangeError: Maximum call stack size exceeded
    at makeCompositeTypeMapper (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:38964:41)
    at combineTypeMappers (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:38974:30)
    at instantiateTypeWorker (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:39273:62)
    at instantiateType (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:39220:26)
    at getMappedType (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:38952:63)
    at /home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:39187:92
    at Object.map (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:335:29)
    at getConditionalTypeInstantiation (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:39187:40)
    at instantiateTypeWorker (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:39273:24)
    at instantiateType (/home/plol/src/wtf/bug/node_modules/typescript/lib/tsc.js:39220:26)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions