In order to face ng2 and the front end of the future, began to Ts,ts is Microsoft out of a language, as a superset of ES6, some of his features are very good (slightly pits). For those of me who have been in front of me (from the beginning, to JavaScript), I really want to say, what the hell is this special? Some features are similar to ES6 (the individual is quite fond of es6). TS the same time coffeescript, will JS Python, TS also put JS engage in other languages.
Chinese study , here is English study . If you do not want to see these things, then read my notes to learn it. Ha ha.
TS base Type, Boolean value, number, string, array, tuple (tuple), enumeration, arbitrary value, null value. The next few you don't know is the type of new addition. See how they define.
TS definition has interface, familiarity with other languages may be known, which is type-safe. The Boolean value is defined as
Boolean false;
The number is, it is important to note that it also supports the ES6 2 and 8 binary.
Let Decliteral:number = 6= 0xf00d== 0o744;
String
Let name:string = "Bob"= "Smith";
Also support ES6 template definition form (not understand es6 see Nanyi es6 get started)
Let name:string == PNs= ' Hello, my name is ${name}. I'll be ${age + 1} years old next month.
Array can be
Let list:number[] = [1, 2, 3];
can also
Let list:array<number> = [1, 2, 3];
Two different ways.
In fact, look at the previous JS original type, the definition of the time is only one more: type, if the variable is assigned to the wrong type, the compiler will error. Type safety Orz. (I'm not accustomed to reading JS). The compiled JS inside is still the original type definition.
See the new type below
meta-group :
// Declare a tuple type Let x: [string, number]; // Initialize It // OK // Initialize It incorrectly // Error
This tuple is an array of known quantities of the contract type. X[0] is a string type, x[1] is the number type, as long as it is not the compiler error.
// OK // Error, ' number ' does not has ' substr '
Here with X[1] is a number type, no substr, the error.
// OK, string can be assigned to (String | number) type Console.log (x[// OK, ' string ' and ' number ' both have toStringx[true// Error, Boolean not (String | number) type
When cross-border access, this can be a string | Number, all the other errors. Above
Enumeration
enum
A type is a supplement to the JavaScript standard data type.
= Color.green;
Let's see what the compiler compiled JS color is.
The C defined here is the color type, the assignment is Color.green, and the figure above is 1. When you take color[1], it's green.
Enum Color {Red = 1= Color.green;
Enum Color {Red = 1, Green = 2, Blue = 4= Color.green;
That's all you can do. Then the corresponding sequence number will also be changed accordingly.
Any value (any)
When you don't know what a variable is, this type of test is not necessary.
Let Notsure:any = 4= "Maybe a string instead"false// Okay, definitely a boolean
That definition of any will skip the compiler's detection.
You may think that Object
there is a similar effect, as it does in other languages. But a Object
variable of type only allows you to assign an arbitrary value to it-but cannot invoke any method on it, even if it does have these methods:
Let Notsure:any = 4// Okay, ifitexists might exist at runtime// Okay, toFixed exi STS (but the compiler doesn ' t check)= 4// error:property ' toFixed ' doesn ' t exist on t Ype ' Object '.
Define an array, and use any when the array type is different
true, "free"];list[1] = 100;
Null value
It means that there is no type, just like void in other languages, to indicate that the function does not return a value.
function void { alert ("This is my warning message");}
Declaring a void
variable of a type is no big use, because you can only give it undefined
and null
:
void = undefined;
Type assertion
Type assertions are analogous to type conversions in other languages, but do not have special data checking and deconstruction. It has no runtime impact, it only works in the compile phase
One is
Let Somevalue:any = "This is a string"= (<string>somevalue). length;
There is also a way to
Let Somevalue:any = "This is a string"= (somevalue as String). length;
The two forms are equivalent. Of course, in the JSX with the first type will not.
Typescript Learning Notes (i) Basic types