-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Stack overflow when inferring recursive type #37475
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue