Augment a Type declaration from a library

Viewed 1554

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.

0 Answers
Related