Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to ignore "Type instantiation is excessively deep and possibly infinite. ts(2589)" typescript error?

Tags:

typescript

After update to Typescript 3.5 i'm getting a lot of "Type instantiation is excessively deep and possibly infinite.ts(2589)" errors.

How can I ignore them?

Where the code happens (using TypeORM)

import { Connection, Repository, Entity, BaseEntity,  createConnection } from 'typeorm';

@Entity()
class MyEntity extends BaseEntity {
    public id: number;
}

class Test {
    async test() {
        const connection: Connection = await createConnection();
        const repo1                       = connection.getRepository(MyEntity); 
        const repo2: Repository<MyEntity> = connection.getRepository(MyEntity); // only here cast the error above
    }
}

I noted that only the repo2 initialization cast the error message.

like image 675
Daniel Santos Avatar asked Sep 05 '19 03:09

Daniel Santos


2 Answers

... You should see an error:

type Test00<T1 extends any[], T2 extends any[]> =
    Reverse<Cast<Reverse<T1>, any[]>, T2>

Type instantiation is excessively deep and possibly infinite. ts(2589)

It happens when TS decides that types become too complex to compute (ie). The solution is to compute the types that cause problems step by step:

type Test01<T1 extends any[], T2 extends any[]> =
    Reverse<Reverse<T1> extends infer R ? Cast<R, any[]> : never, T2>

https://github.com/microsoft/TypeScript/issues/30188#issuecomment-478938437

like image 171
Medet Tleukabiluly Avatar answered Nov 18 '22 08:11

Medet Tleukabiluly


I managed to make the error go away while still being able to compute the type properly with the usage of @ts-ignore. An example can be found in the playground, in a nutshell if you remove the // @ts-ignore, in a full-fledged development environment, line I imagine that the code would not compile.

like image 8
Mateja Petrovic Avatar answered Nov 18 '22 08:11

Mateja Petrovic