typescripttypescript-declarations

Override default lib type in ambient declaration


I'm currently trying to add ambient TypeScript declarations (.d.ts) to a plain JS library that is intended to be used in non-TS environments.

These declarations are imported with the special /// <reference path="app.d.ts" /> directive. This conveniently enables code completion even in code bases where TypeScript is not directly used (due to the LSP TypeScript integration available in most IDEs).

My JS library has a declaration for a global class named Partial but because it collides with the Partial utility type, it results in an error and the editor refuse to autocomplete it.

For clarity, here is how my class ambient declaration looks like:

declare class Partial {
  constructor(data?: { [key:string]: any })
}

Every other declaration works as expected - the editor provide the auto completion and docs on hover.

The only solution I could find was to add /// <reference no-default-lib="true"/> and it works, but this disables the code completion for the default js types (array, strings, etc.).

Is there a way to override/disable concrete types from the default TS library to avoid types conflict?


Solution

  • I've found a workaround inspired from https://github.com/microsoft/TypeScript/issues/27594#issuecomment-566836640.

    interface PartialAlias { ... }
    
    declare const Partial: {
      new(data?: { [key:string]: any }): PartialAlias;
    };