U
U
user_of_toster2021-04-12 06:36:44
typescript
user_of_toster, 2021-04-12 06:36:44

How to assign a type that implements an interface?

There is an interface

Interface MyInterface {
    myMethod(): string;
}

let MyInstance;

Next in the code, I want to indicate that myInstancethis is an instance of the class that implements MyInterface. How can I do that? If you do this:
let myInstance: MyInterface;then all unnecessary properties will give a compilation error, because an object literal is used, for example like this:
myInstance = {myMethod() {return ''}, a: 3} // property 'a' doesnt exist in MyInterface

This one, unfortunately, doesn't work either:
let myInstance: {} extends MyInterface;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2021-04-12
@user_of_toster

MyInterface & { a: number }
But why?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question