Skip to content

"Maximum call stack size exceeded" Involving Mapped Types and Generic Type Inference #20064

Description

@tomblind

TypeScript Version: 2.6.1

Code

type Foo<T> = {
    [K in keyof T]: Foo<T[K]>;
};

interface Bar<T> {
    bar: Foo<T> | Bar<T>;
}

function a<T>(fooBar: Foo<T> | Bar<T>) {
    return b(fooBar); //Doesn't work
//    return b<T>(fooBar); //Works
}

function b<T>(fooBar: Foo<T> | Bar<T>) {
}

tsconfig.json

{
    "compilerOptions":
    {
        "target": "ES2015",
        "strict": true
    }
}

Expected behavior:
Should compile successfully.

Actual behavior:
Compiler crashes with the following output:

tsc.js:58005
                throw e;
                ^

RangeError: Maximum call stack size exceeded
    at instantiateType (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:25752:33)
    at instantiateType (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:25779:49)
    at C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:25603:42
    at Object.map (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:469:29)
    at getAnonymousTypeInstantiation (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:25688:40)
    at instantiateType (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:25763:32)
    at getTemplateTypeFromMappedType (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:23662:21)
    at inferTypeForHomomorphicMappedType (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:27543:32)
    at inferFromObjectTypes (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:27757:48)
    at inferFromTypes (C:\Users\blind\projects\pss\acorn\node_modules\typescript\lib\tsc.js:27717:29)

The issue seems to be a problem with inferring the generic parameter when calling b() inside of a(). Explicitly supplying the parameter fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDuplicateAn existing issue was already created

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions