简体   繁体   中英

Typescript Add types to libraries method arguments

I am using an external library pg-mem which exposes LibAdapters and it has a method createTypeormConnection which accepts few arguments but of type any .

export interface LibAdapters {
  createTypeormConnection(typeOrmConnection: any, queryLatency?: number): any;
}

I want to specify the exact type of these arguments as I am getting a lot of lint errors:

Unsafe return of an `any` typed value.eslint@typescript-eslint/no-unsafe-return

How can I specify the types of the arguments & return type of the method?

You can extend exisiting types by using declaration merging :

declare module "pg-mem" {
    interface LibAdapters {
        createTypeormConnection(typeOrmConnection: SomeType, queryLatency?: number): SomeOtherType
    }
}

Note, that this will add overload of createTypeormConnection instead of overriding it, so if you call createTypeormConnection with value of type other than SomeType , you will still get any .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM