Typescript Add types to libraries method arguments

Viewed 78

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?

1 Answers

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.

Related