Typescript - declaring a variable as a type which extends an interface

1k Views Asked by At

Let's say I have an interface called MyIFace. Is there any way to declare a member property of a type which extends that very interface using type annotation? I try to post a pseudo-code of what I'm searching for:

class Foo {
    x: any extends MyIface;               //this is what I'd like to write

    // some code here...
}

This previous question talks about intersection types, which apparently seems to be fine (the compiler doesn't complain) but in the end it's not what I need. Indeed, declaring x: any & MyIFace; doesn't resemble the structure I need: it states that x has both any and MyIFace blueprints, while I want to state that x has a blueprint of any which extends the blueprint of MyIFace.

1

There are 1 best solutions below

5
On

Correct me if I'm wrong, but I believe you just need:

class Foo {
    x?: MyIface & Record<string, any>;
    // some code here...
}