Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 694
Seph Reed Avatar asked Nov 23 '25 04:11

Seph Reed


1 Answers

"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

like image 65
Acid Coder Avatar answered Nov 28 '25 02:11

Acid Coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!