Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what kind of Anonymous Classes syntax is this in typescript?

interface Myinterface extends interface1 {
    name: string;
}

let mylet = <Myinterface >{};

mylet.name = "nana";
 

what is < Myinterface>{} ??
Is this one of the ways to create an object without a class?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s a type assertion, another way of writing:

let mylet = {} as Myinterface;

It says "The object ({}) I’m assigning to mylet is of type Myinterface" (even though, in that code, it isn’t — yet, not until you add the name property).

The <Myinterface>{} version is less common these days because of JSX, which wants to use <Myinterface> to start an element.

Given the question, I’m guessing you didn’t write that code. For what it’s worth, that code would be better written as:

let mylet = {
    name: "nana",
};

That has the same result as:

let mylet = {} as Myinterface;
mylet.name = "nana";

…since TypeScript’s type system is structural (based on shapes), not nominal (based on names).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading