TypeScript syntax (1) -- Basic Data Type
1. Boolean)
var isDone: boolean = true;
2. Number)
var height: number = 6;
Iii. String type)
You can use double quotation marks or single quotation marks.
var name: string = "bob";name = 'smith';
4. Array)
Two methods to declare an array:
First:
var list:number[] = [1,2,3];
Note:numberIndicates the Data Type of elements in the array,[]Declares an array.
Second:
var list:Array
= [1,2,3];
Note:ArrayDeclares an array, Indicates the Data Type of elements in the array.
5. Enumeration type (Enum)
enum Color {Red, Green, Blue};var c: Color = Color.Green;
By default, the index starting with a member of the enumeration type is0(RedThe index is0), You can manually change the start index of the member in the enumeration type, for example:
enum Color {Red = 1, Green, Blue};var c: Color = Color.Green;
RedThe index is1You can also specify an index for each member, for example
Enum Color {Red = 1, Green = 2, Blue = 4}; var c: Color = Color. Green; // note c: there is no space between each character of Color.
Value Through Index
enum Color {Red = 1, Green, Blue};var colorName:String = Color[2];alert(colorName);
6. Any type (Any)
A variable that describes unknown types, or the variable type changes dynamically. It must be declaredanyType to make the variable pass the variable type check during compilation.
var notSure: any = 4;notSure = "maybe a string instead";notSure = false;
Arrays containing different types of elements:
var list:any[] = [1, true, "free"];list[1] = 100;
7. No type (void)
The following function does not return any value
function warnUser(): void { alert("This is my warning message");}