Typescript: "The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed."

Viewed 2332

enter image description here

I'm getting the error

"The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed."

This was working fine a few hours ago... nothing that I've changed (update npm and node) makes any sense as to why this would happen.

I absolutely can not give an explicit type annotation.


What I've tried:

This is my third complete rewrite of the tool I'm trying to make because Typescript keeps lagging out about it.

After three full rewrites, hundreds of hours, and extreme effort everything had been working fine for one whole day before this error started popping up.

I've tried updating everything (typescript, node, npm... nothing else to update)

I've tried updating tsconfig:

"compilerOptions": {
        "disableSizeLimit": true,

I have no idea what to do. The only alternative I can imagine is throwing Typescript in the trash and doing type checking at compile time... which would be fine for this project I guess.


On attempt 2 of this project, I asked about better caching

2 Answers

Okay, I've managed to drag this project on just a little bit further. Here is the primary change I made:

Convert interface unions/intersections to class unions/intersections

I don't know why this works, but if you have some complex union/intersection like so:

interface A { 
  a: true,
  // code omitted...
}

interface B { 
  b: true,
  // code omitted...
}

// code omitted...

type ComplexIntersection = A & B & C & D & E & F & G;

You can switch all the interfaces to abstract classes like so:

abstract class A { 
  public abstract a: true;
  // code omitted...
}

abstract class B { 
  public abstract b: true;
  // code omitted...
}

And the Typescript language server will be happier about it. I have no idea why this works, but here's what the type looks like

before: Before

and after: After


Other things I tried that didn't work:

  1. Removing every complex type I could find
  2. Removing every generic argument I could

"declaration": false,

set declaration to false in tsconfig.json works for me

also found related topic on github https://github.com/microsoft/TypeScript/issues/43817

this is caused by recursion, which is also my case

update: apparently this is the real solution:

type Builder<T> = {
    withComment: (comment: string) => Builder<T & { comment: string }>,
    withSpecialComment: () => Builder<T & { comment: string }>,
    withTrackingNumber: (tracking: number) => Builder<T & { tracking: number }>,
    build: () => T
};

const Builder: <T extends {}>(model: T) => Builder<T> = <T extends {}>(model: T) => ({
    withComment: (comment: string) => Builder({ ...model, comment }),
    withSpecialComment: () => Builder(model).withComment('Special comment'),
    withTrackingNumber: (tracking: number) => Builder({ ...model, tracking }),
    build: () => model });

const a = Builder({});

https://github.com/microsoft/TypeScript/issues/43817#issuecomment-827746462

with this you don't need to set declaration to false

Related