Search code examples
typescripttypescript-typingsdefinitelytypedtypescript-types

Extending @types - delete field from interface, add type to field in interface


I have javascript library with types from npm/@types.

I need to make two fixes to @types which applies only in case of my application, so I can't merge them into DefinitelyTyped repository.

I need to:

  1. remove one of fields from interface. Example:

    // before changes:
    interface A {
            a?:string;
            b?:string;
            c?:string;
    }
    
    // after changes:
    interface A {
            a?:string;
            c?:string;
    }
    
  2. add more types to one field in interface. Example:

    // before changes:
    interface B {
            a?: C;
    }
    
    // after changes:
    interface B {
            a?: C | D;
    }
    

Also I still want to download main @types definitions from external repository.

What is the best way to achieve this?


Solution

  • This can be solved using the following method.

    import { A as AContract, B as BContract, C, D } from './contracts.ts';
    
    // Removes 'b' property from A interface.
    interface A extends Omit<AContract, 'b'> { }
    
    interface B extends BContract {
      a?: C | D;
    }