Typescript: [TSC fails] RangeError: Maximum call stack size exceeded [with repro repo]

Created on 3 May 2019  路  5Comments  路  Source: microsoft/TypeScript

TypeScript Version: Version 3.5.0-dev.20190503

Search Terms:

tsc, RangeError, Maximum call stack size exceeded

Code

import { ObjectTypeComposer } from 'graphql-compose';
declare const User: ObjectTypeComposer<any, any>;
User.addResolver({ type: User });

Dramatically simplified graphql-compose.d.ts:


export type ObjMapReadOnly<T> = Readonly<{ [key: string]: Readonly<T> }>;
export type Thunk<T> = (() => T) | T;

export type ComposeOutputTypeDefinition = Readonly<ObjectTypeComposer<any, any> | EnumTypeComposer>;

export class EnumTypeComposer {
  public setFields(fields: { [name: string]: { [key: string]: any } }): this;
}

export class ObjectTypeComposer<TSource, TContext> {
  public setFields(fields: ObjMapReadOnly<Resolver>): this;

  public addResolver<TResolverSource>(opts: { type?: Thunk<ComposeOutputTypeDefinition> }): this;
}

export class Resolver {
  public wrapArgs<NewContext>(
    cb: () => {
      [argName: string]: Thunk<Readonly<EnumTypeComposer>>;
    }
  ): void;

  public wrapType(cb: () => ComposeOutputTypeDefinition): void;
}

Expected behavior:

Do not fall with Maximum call stack size exceeded error.

Actual behavior:

> [email protected] tsc /Users/nodkz/www/_sandbox/nodkz-tsc-problem
> tsc

/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:72973
                throw e;
                ^

RangeError: Maximum call stack size exceeded
    at __generator (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:30:13)
    at getUnmatchedProperties (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37203:20)
    at getUnmatchedProperty (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37237:20)
    at typesDefinitelyUnrelated (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37245:19)
    at inferFromObjectTypes (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37544:22)
    at inferFromTypes (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37456:29)
    at inferFromContravariantTypes (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37475:21)
    at applyToParameterTypes (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37047:17)
    at inferFromSignature (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37603:21)
    at inferFromSignatures (/Users/nodkz/www/_sandbox/nodkz-tsc-problem/node_modules/typescript/lib/tsc.js:37595:21)

Playground Link:

Simplified repro repo: https://github.com/nodkz/tsc-problem

git clone https://github.com/nodkz/tsc-problem ./nodkz-tsc-problem \
  && cd nodkz-tsc-problem \
  && npm install \
  && npm run tsc
Bug Crash Fixed

Most helpful comment

As a workaround I relaxed the checks in my lib:

export type ObjMapReadOnly<T> = Readonly<{ [key: string]: Readonly<T> }>;
- export type Thunk<T> = (() => T) | T;
+ export type Thunk<T> = (() => any) | T;

export type ComposeOutputTypeDefinition = Readonly<ObjectTypeComposer<any, any> | EnumTypeComposer>;

export class EnumTypeComposer {
  public setFields(fields: { [name: string]: { [key: string]: any } }): this;
}

export class ObjectTypeComposer<TSource, TContext> {
  public setFields(fields: ObjMapReadOnly<Resolver>): this;

  public addResolver<TResolverSource>(opts: { type?: Thunk<ComposeOutputTypeDefinition> }): this;
}

export class Resolver {
  public wrapArgs<NewContext>(
    cb: () => {
      [argName: string]: Thunk<Readonly<EnumTypeComposer>>;
    }
  ): void;

  public wrapType(cb: () => ComposeOutputTypeDefinition): void;
}

It's better than nothing "maximum call stack size exceeded"

photo_2019-11-25 12 21 30

All 5 comments

@orta, please help to fix this issue. It blocks me from upgrading Gatsby and other small projects.

Thanks.

I have the same issue on TypeScript v3.6.4

Same issue on TS 3.7.2.

As a workaround I relaxed the checks in my lib:

export type ObjMapReadOnly<T> = Readonly<{ [key: string]: Readonly<T> }>;
- export type Thunk<T> = (() => T) | T;
+ export type Thunk<T> = (() => any) | T;

export type ComposeOutputTypeDefinition = Readonly<ObjectTypeComposer<any, any> | EnumTypeComposer>;

export class EnumTypeComposer {
  public setFields(fields: { [name: string]: { [key: string]: any } }): this;
}

export class ObjectTypeComposer<TSource, TContext> {
  public setFields(fields: ObjMapReadOnly<Resolver>): this;

  public addResolver<TResolverSource>(opts: { type?: Thunk<ComposeOutputTypeDefinition> }): this;
}

export class Resolver {
  public wrapArgs<NewContext>(
    cb: () => {
      [argName: string]: Thunk<Readonly<EnumTypeComposer>>;
    }
  ): void;

  public wrapType(cb: () => ComposeOutputTypeDefinition): void;
}

It's better than nothing "maximum call stack size exceeded"

photo_2019-11-25 12 21 30

Possible alternative repro #35804

Was this page helpful?
0 / 5 - 0 ratings

Related issues

siddjain picture siddjain  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

dlaberge picture dlaberge  路  3Comments

remojansen picture remojansen  路  3Comments