TypeScript is a superset of JavaScript, and TypeScript generates JavaScript code after it has been compiled. The biggest feature of TypeScript is the type, which is called TypeScript. Typed TypeScript are easier to maintain than weakly typed JavaScript.
There are 7 basic types in TypeScript.
1, Boolean
var Boolean false;
2. Number
Represents a number in JavaScript. In JavaScript, both "integer" and "floating point" are stored as double-precision floating-point types.
var height:number = 6;
3, String
Represents a string. Like JavaScript, you can use a pair of double quotation marks (") or a pair of single quotation marks (') to represent a string.
var name:string = "Bob"= ' Smith ';
4. Array
There are two methods of array declaration in TypeScript.
① uses "[]" to declare:
var list:number[] = [1, 2, 3];
② uses an array type to declare:
var list:array<number> = [1, 2, 3];
Both declarations can be used, and the effect will not be different. However, the recommended code should try to use only one of them to keep the code style uniform.
5. Enum
The enumeration type is newly added in TypeScript, and there is no such type in JavaScript.
enum Color { Red, Green, Blue}; var c:color = Color.green;
Like C #, if the value of the first item is not declared, then the value of Red above is 0, then each item is incremented by one, that is, Green is 1,blue is 2.
enum Color { = 1, Green, Blue}; var c:color = Color.green;
So the value of Red at this time is 1,green to 2,blue of 3.
Of course, you can also specify a value for each item.
enum Color { = 1, = 2, = 4}; var c:color = Color.green;
In addition, there is a special function of enumeration type, if we have a numeric value, but we do not know whether the enumeration type is defined, can be obtained in the following ways:
enum Color { = 1, Green, Blue}; var colorname:string = color[2= color[4];alert (colorname);
Then the Green and undefined will be output. Because the value of Green is 2, and none of the enumeration defines a value of 4, it returns undefined.
6. Any
As with the default type of variables in JavaScript, the reference is dynamic and can be assigned to any type. For example:
var notsure:any = 4= "Maybe a string instead"false// Okay, definitely a Boolean
After you define any, you lose the functionality of syntax-aware, which is equivalent to writing JavaScript.
It is worth mentioning that any can be used with arrays:
var true, "free"];list[1] = 100;
7. void
This type can only be used in functions, and the return type of the function can be specified as void, indicating that the function does not return any values.
function void { alert ("This is my warning message");}
TypeScript TypeScript Learn the basic types of the elements