Home > Mobile >  In a definition file, is it possible to extend from an imported module directly?
In a definition file, is it possible to extend from an imported module directly?

Time:09-23

In a d.ts file, I'd like to do the following:

interface A extends import("some-module").B
{
   //...
}

The only way I've been able to do it is by importing the type I'm extending from first:

type ExternalB = import("some-module").B

interface A extends ExternalB
{
    //...
}

CodePudding user response:

This isn't allowed, and as in the Playground, it gives error #2499.

An interface can only extend an identifier/qualified-name with optional type arguments. (2499)

This is also consistent with similar questions on SO: You have to define the type first. As a hunch, this is probably for compatibility with the ECMAScript extends definition.

You can, however, do a type intersection dynamically. You can even extend the resulting type as an interface, assuming the result is an object type.

type A2 = import("some-module").B & {
    a2(): void;
}
interface A3 extends A2 {
    a3(): void;
}

Playground Link

  • Related