Basic Type
1. Boolean
let isDone: Boolean = false;
- 1
2. Number (number)
let num: number = 123;
let num2: number = 0xf00d;
- 1
- 2
3. String
let person: string = "Xiao Ming";
person = 'Xiaoxiao'
let person2: string = "Xiaohua";
let age = 20;
let sentence: string = `Hello,my name is ${person2}. I'll be ${age} years old next month.`;
let sentence2: string = "Hello,my name is" + person + ".\n\n" + "I'll be" + (age + 1) + "years old next month.";
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
4.Array
let list: number[] = [1, 2, 3];
let list2:Array<number> = [1, 2, 3];
- 1
- 2
- 3
5. Tuple (Tuple)(Tuople type allows an array of known elements and types, and the types of each element do not have to be the same.)
let x: [string, number];
x = ['hello', 10];
// x = [20, "hello"] Error
// x = ["hello", 20 ,30] Error
(x[0].substr(1));
// (x[1].substr(1)); Error
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
6. Enum (enum)(enum type is correctJavaScriptA supplement to standard data types. Like other languages such as C#, useEnumeration typeA set of values can be given a friendly name. )
enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];
- 1
- 2
One convenience that the enum type provides is that you can get its name by the enum. For example, we know that the value is 2, but we are not sure which name it maps to in Color, we can find the corresponding name:
alert(colorName); // Show 'Green' because its value in the above code is 2
- 1
7.Arbitrary value (any)
let notSure: any = 4;
notSure = 'maybe a string instead'
notSure = false
- 1
- 2
- 3
When rewriting existing code,anyTypes are useful, allowing you to optionally include or remove type checking at compile time. You might think that Object has a similar effect, just like it does in other languages. But a variable of type Object just allows you to assign arbitrary values to it - but cannot call any method on it, even if it really has these methods
();
();
let prettySure: Object = 4;
// (); //The attribute "toFixed" does not exist on the type "Object".
- 1
- 2
- 3
- 4
- 5
Any type is also useful when you only know the type of part of the data. For example, you have an array that contains different types of data:
let arr: any[] = [1, true, "free"];
arr[1] = 100;
- 1
- 2
- 3
8.Null value (void)(To some extent, the void type is like the opposite of any type, it means there is no type. When afunctionWhen there is no return value, you usually see that its return value is void)
function warnUser(): void{
return alert("this is my message");
}
- 1
- 2
- 3
Declaring a void type variable is of no use, because you can only give it undefined
let unusable: void = undefined;
- 1
and undefined(Undefined and null have their own types called undefined and null respectively. Similar to void, their own types are not very useful)
let u:undefined = undefined;
let n:null = null;
- 1
- 2
(The never type represents the types of values that never exist. For example, the never type is those that always throwabnormalor there will be no function expressions that return values orArrow functionThe return value type of the expression)
The function returning never must have an unattainable endpoint
function error(message:string): never{
throw new Error(message)
}
- 1
- 2
- 3
- 4
The inferred return value type is never
function fail():never {
return error("Something failed");
}
- 1
- 2
- 3
- 4
The function returning never must have an unattainable endpoint
function infiniteLoop(): never {
while (true) {
}
}
- 1
- 2
- 3
- 4
- 5
// function get(): never{ //Error The function returning "never" cannot have an accessible endpoint
// alert(123)
// }
- 1
- 2
- 3
(object represents a non-primitive type, that is, except number,stringtypes other than boolean, symbol, null or undefined)
declare function create(o: object | null ): void;
create({ prop: 0 });
create(null);
// create(1234)//Error
// create("string"); // Error
// create(false); // Error
// create(undefined); // Error
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Type Assertion
1. Angle bracket method:
let someValue:any = "this is a string";
let strlength:number = (<string>someValue).length;
- 1
- 2
grammar:
let someValue2:any = "this is a string";
let strlength2:number = (someValue2 as string).length
- 1
- 2
The two forms are equivalent. As for which one is used in most cases, it is based on personal preference; however, when you use JSX in TypeScript, only the assertion is allowed.
- 1