4

Unfortunately I have to use a library that has errors in the defined types / interfaces. Some fields are missing, some are optional some are wrong etc. I would like to override / extend these types / interfaces declarations.

For example, this is how a interfaces/types looks like in the library:

export declare interface Apple {
       color: string
}

export declaration type Bananas = string

I would like to extend it with additional fields or redeclare without changing its names like this:

augmentations.d.ts file :

import { Apple as AppleInterface } from "some_npm_library" 

declare module "some_npm_library" {

export declare interface Apple extends AppleInterface {
       size: string
    }

export declare type Bananas = number

}

Unfortunately, the TS is referring to the library declaration and not to my declarations. Is it possible to redeclare without changing the interface name? What am I doing wrong?

  • You can add properties to existing interfaces via [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html), but type aliases are essentially frozen; so you might have to copy the library files, modify the copies, and use only those. Essentially: fix the broken things in the files where they are broken, and possibly even request such fixes be pulled upstream. Reminds me of [this](https://stackoverflow.com/q/68297512/2887218) or [this](https://stackoverflow.com/q/48690619/2887218). – jcalz Oct 16 '21 at 02:16

0 Answers0