简体   繁体   中英

what is __type keyword in typescript?

interface IAnotherService extends Service1<string> {
  __type: "com.east.service#anotherservice";
}

My question is what is the "__type" keyword? What does it do?

It's not a keyword; it's just the name of a property in an interface, like bar in

interface Foo { 
  bar: string; 
}

So if you have a value v of type IAnotherService , then you can presumably index into it like v.__type :

declare const v: IAnotherService;
v.__type; // okay

Without more context it's hard to be sure if this is a real property (you can access it at runtime) or phantom property (it is not really there but you just use it so TypeScript can keep track of extra type information not present at runtime). The double underscore is a hint to me that it might be the latter. One use of phantom properties is to simulate nominal typing, known as "branding" a type; see this FAQ entry for more information.

Playground link to code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM