Skip to content

New error: Type instantiation is excessively deep and possibly infinite. #37613

Open
@amcasey

Description

@amcasey

This one's actually new in 3.8. There are some additional errors in 3.9 - I'll post them below.

https://github.com/davidkpiano/xstate/blob/2e0bd5dcb11c8ffc5bfc9fba1d3c66fb7546c4bd/packages/core/src/stateUtils.ts#L157

packages/core/src/stateUtils.ts:157:36 - error TS2589: Type instantiation is excessively deep and possibly infinite.

157   return getValueFromAdj(rootNode, getAdjList(config));
                                       ~~~~~~~~~~~~~~~~~~

packages/core/src/State.ts:181:12 - error TS2589: Type instantiation is excessively deep and possibly infinite.

181     return new State(config);
               ~~~~~~~~~~~~~~~~~

packages/core/src/interpreter.ts:208:5 - error TS2589: Type instantiation is excessively deep and possibly infinite.

208     this.parent = parent;
        ~~~~~~~~~~~~~~~~~~~~

packages/core/src/interpreter.ts:783:13 - error TS2345: Argument of type '{ type: string; data: any; }' is not assignable to parameter of type 'string | EventObject | Event<EventObject>[] | Event<EventObject>'.
  Object literal may only specify known properties, and 'data' does not exist in type 'EventObject'.

783             data: err
                ~~~~~~~~~

To repro:

  1. yarn
  2. tsc -b -f tsconfig.monorepo.json

Activity

added this to the TypeScript 3.9.1 milestone on Mar 25, 2020
amcasey

amcasey commented on Mar 25, 2020

@amcasey
MemberAuthor

3.9 errors:

packages/core/src/stateUtils.ts:157:36 - error TS2589: Type instantiation is excessively deep and possibly infinite.

157   return getValueFromAdj(rootNode, getAdjList(config));
                                       ~~~~~~~~~~~~~~~~~~

packages/core/src/actions.ts:108:9 - error TS2783: 'type' is specified more than once, so this usage will be overwritten.

108         type,
            ~~~~

  packages/core/src/actions.ts:109:9
    109         ...exec,
                ~~~~~~~
    This spread always overwrites this property.

packages/core/src/State.ts:181:12 - error TS2589: Type instantiation is excessively deep and possibly infinite.

181     return new State(config);
               ~~~~~~~~~~~~~~~~~

packages/core/src/interpreter.ts:208:5 - error TS2589: Type instantiation is excessively deep and possibly infinite.

208     this.parent = parent;
        ~~~~~~~~~~~~~~~~~~~~

packages/core/src/interpreter.ts:783:13 - error TS2345: Argument of type '{ type: string; data: any; }' is not assignable to parameter of type 'string | EventObject | Event<EventObject>[] | Event<EventObject>'.
  Object literal may only specify known properties, and 'data' does not exist in type 'EventObject'.

783             data: err
                ~~~~~~~~~

packages/core/src/StateNode.ts:285:5 - error TS2322: Type 'MachineOptions<TContext, any> & Partial<MachineOptions<TContext, TEvent>>' is not assignable to type 'MachineOptions<TContext, TEvent>'.
  Types of property '_parent' are incompatible.
    Type '(StateNode<TContext, any, any, any> & StateNode<TContext, any, TEvent, any>) | undefined' is not assignable to type 'StateNode<TContext, any, TEvent, any> | undefined'.
      Type 'StateNode<TContext, any, any, any> & StateNode<TContext, any, TEvent, any>' is not assignable to type 'StateNode<TContext, any, TEvent, any> | undefined'.
        Type 'StateNode<TContext, any, any, any> & StateNode<TContext, any, TEvent, any>' is not assignable to type 'StateNode<TContext, any, TEvent, any>'.
          Types of property 'parent' are incompatible.
            Type '(StateNode<TContext, any, any, any> & StateNode<TContext, any, TEvent, any>) | undefined' is not assignable to type 'StateNode<TContext, any, TEvent, any> | undefined'.
              Type 'StateNode<TContext, any, any, any> & StateNode<TContext, any, TEvent, any>' is not assignable to type 'StateNode<TContext, any, TEvent, any> | undefined'.

285     this.options = Object.assign(createDefaultOptions<TContext>(), options);
        ~~~~~~~~~~~~

packages/core/src/StateNode.ts:1824:7 - error TS2322: Type 'TransitionsConfigArray<TContext, TEvent>' is not assignable to type '(TransitionConfig<TContext, EventObject> & { event: string; })[]'.
  Type '(TransitionConfig<TContext, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject> & { ...; }) | (TransitionConfig<...> & { ...; }) | (TransitionConfig<...> & { ...; })' is not assignable to type 'TransitionConfig<TContext, EventObject> & { event: string; }'.
    Type 'TransitionConfig<TContext, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject> & { ...; }' is not assignable to type 'TransitionConfig<TContext, EventObject> & { event: string; }'.
      Type 'TransitionConfig<TContext, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject> & { ...; }' is not assignable to type 'TransitionConfig<TContext, EventObject>'.
        Types of property 'target' are incompatible.
          Type 'string | StateNode<TContext, any, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject, any> | (string | StateNode<...>)[] | undefined' is not assignable to type 'string | StateNode<TContext, any, EventObject, any> | (string | StateNode<TContext, any, EventObject, any>)[] | undefined'.
            Type 'StateNode<TContext, any, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject, any>' is not assignable to type 'string | StateNode<TContext, any, EventObject, any> | (string | StateNode<TContext, any, EventObject, any>)[] | undefined'.
              Type 'StateNode<TContext, any, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject, any>' is not assignable to type 'StateNode<TContext, any, EventObject, any>'.
                The types of 'config.on' are incompatible between these types.
                  Type 'TransitionsConfigMap<TContext, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject> | TransitionsConfigArray<...> | undefined' is not assignable to type 'TransitionsConfigMap<TContext, EventObject> | TransitionsConfigArray<TContext, EventObject> | undefined'.
                    Type 'TransitionsConfigMap<TContext, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject>' is not assignable to type 'TransitionsConfigMap<TContext, EventObject> | TransitionsConfigArray<TContext, EventObject> | undefined'.
                      Type 'TransitionsConfigMap<TContext, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject>' is not assignable to type 'TransitionsConfigMap<TContext, EventObject>'.
                        Type 'SingleOrArray<string | StateNode<TContext, any, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject, any> | (TransitionConfig<...> & { ...; }) | undefined>' is not assignable to type 'SingleOrArray<string | StateNode<TContext, any, EventObject, any> | (TransitionConfig<TContext, EventObject> & { ...; }) | undefined>'.
                          Type 'StateNode<TContext, any, ActionTypes.NullEvent extends TEvent["type"] ? Extract<TEvent, { type: TEvent["type"] & ActionTypes.NullEvent; }> : EventObject, any>' is not assignable to type 'SingleOrArray<string | StateNode<TContext, any, EventObject, any> | (TransitionConfig<TContext, EventObject> & { ...; }) | undefined>'.

1824       onConfig = this.config.on;
           ~~~~~~~~

packages/core/src/StateNode.ts:1824:7 - error TS2589: Type instantiation is excessively deep and possibly infinite.

1824       onConfig = this.config.on;
           ~~~~~~~~~~~~~~~~~~~~~~~~~
ahejlsberg

ahejlsberg commented on Mar 26, 2020

@ahejlsberg
Member

I took a quick look. The excessively depth errors are caused by these two types in src\types.ts:

type TransitionsConfigMap<TContext, TEvent extends EventObject> = {
  [K in TEvent['type'] | NullEvent['type'] | '*']?: SingleOrArray<
    | TransitionConfigTargetShortcut<TContext, TEvent>
    | (TransitionConfig<
        TContext,
        K extends TEvent['type'] ? Extract<TEvent, { type: K }> : EventObject
      > & {
        event?: undefined;
      })
  >;
};

type TransitionsConfigArray<TContext, TEvent extends EventObject> = Array<
  {
    [K in TEvent['type'] | NullEvent['type'] | '*']: TransitionConfig<
      TContext,
      K extends TEvent['type'] ? Extract<TEvent, { type: K }> : EventObject
    > & {
      event: K;
    };
  }[TEvent['type'] | NullEvent['type'] | '*']
>;

The problem appears to be exploration of the conditional type constrains that keep spiraling until we hit the depth limiter. When I change K extends TEvent['type'] ? Extract<TEvent, { type: K }> : EventObject to just EventObject in each of the types, the check time drops by 50% and the type count drops by 60-70%. I'm not exactly sure what these types are doing, but simplifying them definitely has a major effect.

DanielRosenwasser

DanielRosenwasser commented on Mar 27, 2020

@DanielRosenwasser
Member
davidkpiano

davidkpiano commented on Mar 29, 2020

@davidkpiano

Working on a fix for this, but I found some odd type behavior:

type ResolvedEvent = ({
    type: "CHANGE";
    value: string;
} & {
    type: "CHANGE";
}) | ({
    type: "SAVE";
} & {
    type: "CHANGE";
})

function assign(event: ResolvedEvent) {
    return event.value;
}

This will produce the following error for event.value:

Property 'value' does not exist on type 'ResolvedEvent'.
  Property 'value' does not exist on type '{ type: "SAVE"; } & { type: "CHANGE"; }'.(2339)

I would expect ResolvedEvent to be narrowed to { type: 'CHANGE'; value: string } since { type: 'SAVE' } & { type: 'CHANGE' } should be never.

davidkpiano

davidkpiano commented on Mar 31, 2020

@davidkpiano

Ah, no, I was using 3.8.3. Glad to see it's fixed - was that specific issue tracked?

ahejlsberg

ahejlsberg commented on Mar 31, 2020

@ahejlsberg
Member

Glad to see it's fixed - was that specific issue tracked?

Yes, it was fixed by #36696.

vasilii-kovalev

vasilii-kovalev commented on Jul 9, 2021

@vasilii-kovalev

I'm facing the error in such a case:

type GetLetters<Text> = Text extends `${infer Letter}${infer Rest}` ? Letter | GetLetters<Rest> : never;
type Letters = GetLetters<"some very, very long text">;

The error appears when the text is longer than or equal to 24 characters.

Playground.

2 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @DanielRosenwasser@davidkpiano@ahejlsberg@RyanCavanaugh@vasilii-kovalev

        Issue actions

          New error: Type instantiation is excessively deep and possibly infinite. · Issue #37613 · microsoft/TypeScript