Skip to content

infinite type in intersection crashes with "Maximum call stack size exceeded" #16233

@RafaelSalguero

Description

@RafaelSalguero

TypeScript Version: 2.3.4
Code
(default tsc --init used)

interface TestStoreType {
    person: {
        names: {
            lastnames: {
                first: string,
                second: string;
            }
            firstname: string;
        }
        age: number;
    }
    other: string;
}

function path<T, TKey extends keyof T>(parent: string[] , key: TKey) {
    const newPath = [... parent, key];
    let x = (<TSubKey extends keyof T[TKey]>(subkey: TSubKey) => path<T[TKey], TSubKey>(newPath, subkey));
    type TResult = typeof x & { path: string[] };
    let y = x as TResult;
    return y;
}
class PathCheck<T>{
    path<TKey extends keyof T>(key: TKey) {
        return path<T, TKey>([], key);
    }
}

const x = new PathCheck<TestStoreType>();
//Should only allow correct deep property paths:
const y = x.path("person")("names")("lastnames")("first");

Expected behavior:
The program should compile and x.path calls should only allow deep property path names as its arguments

Actual behavior:
Typescript crashes with the following error:

RangeError: Maximum call stack size exceeded
    at instantiateType (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:27176:33)
    at instantiateList (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:26936:33)
    at instantiateTypes (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:26943:20)
    at instantiateTypeNoAlias (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:27210:44)
    at instantiateType (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:27184:24)
    at instantiateSignature (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:27050:231)
    at instantiateList (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:26936:33)
    at instantiateSignatures (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:26946:20)
    at resolveAnonymousTypeMembers (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:25050:38)
    at resolveStructuredTypeMembers (C:\Users\rafael\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:25185:25)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions