typescriptcompiler-optimization

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


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


Solution

  • "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