0

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
{
    //...
}
2
  • 1
    What's wrong with your second example? Why are you looking for alternatives?
    – Alex Wayne
    Commented Sep 22, 2021 at 19:33
  • There's nothing wrong with it, and it works fine. I'm just looking for a cleaner solution that doesn't involve creating new types.
    – Jordan L
    Commented Sep 22, 2021 at 20:54

1 Answer 1

3

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

1
  • I suspected as much - thanks for the additional info.
    – Jordan L
    Commented Sep 22, 2021 at 21:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.