Index Type

1. index type query operators keyof, by keyof we can get all the properties of the composition of the name of a type union type 

keyofOperator, a connection type, a combined type will return all the property names in this type of composition

Info {interface 
  name: String; 
  Age: Number; 
} 
the let infoProp: keyof Info; 
infoProp = "name" ; 
infoProp = "Age" ; 
infoProp = "NO"; // error type can not be "" no "", assigned to the "" name "|" age " "

Here keyof Infois the equivalent"name" | “age”

 

2. The index access operator is [], in fact, and we visit a property value of the object is the same syntax

Info {interface 
  name: String; 
  Age: Number; 
} 
type NameType = Info [ "name" ]; 
the let name: NameType = 123; // error type can not be "123" is assigned to the type "string"

 

NameType = Info type [ "name" ]; get a strinG
123 is the number 

Guess you like

Origin www.cnblogs.com/guangzhou11/p/11323494.html