I have imported a library mycoollib which resides in node_modules and has the following folder and file structure
node_modules
- mycoollib
- src
mycoollogic.js
- types
index.d.ts
The index.d.ts, which contains the type definitions of mycoollib, is this
/// <reference types="node" />
export type MyCoolType = {
func1: () => void
func2: () => void
}
In my application I need to augment MyCoolType type definition, adding for instance a third function. So I create my-cool-type-augmentation.d.ts file in my src folder like this
declare namespace mycoollib {
export type MyCoolType = {
augmentedFunction: () => void
}
}
but this solution seems not to work, since the compiler signals an issue when I try to use augmentedFunction on objects of type MyCoolType.
I have tried also using the module declaration, like this
declare module "mycoollib" {
export type MyCoolType = {
augmentedFunction: () => void
}
}
but in this case the compiler signals the fact that there is a Duplicate identifier 'MyCoolType' error in index.d.ts and in my-cool-type-augmentation.d.ts. This solution works for me in other cases where I have to augment interfaces, but does not seem to work with types.
It's not working because it is not supposed to work according to documentation:
... the key distinction is that a type cannot be re-opened to add new properties vs an interface which is always extendable.
Differences Between Type Aliases and Interfaces
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With