Matters needing attention
-
Automatic type judgment
The TS compiler automatically determines the type of a variable when it is declared and assigned at the same time, so if you are declaring and assigning at the same time, you can omit the type declaration
-
If you do not specify a type for a variable, the TS parser automatically determines that the variable is of type ANY
let a; // The type of a is any
- D is of type any, and it can be assigned to any variable
let s:string;
let d;
d= 10;
s = d; // No error will be reported
Copy the code
Type:
type | example | describe |
---|---|---|
number | 1, 33, 2.5 | Any Numbers |
string | ‘hi’, “hi”, hi |
Arbitrary string |
boolean | True, false, | Boolean value true or false |
literal | In its own right | The value of the constraint variable is the value of the literal |
any | * | Any type |
unknown | * | Type safe any |
void | Null value (undefined) | No value (or undefined) |
never | There is no value | It can’t be any value |
object | {name:’ Sun Wukong ‘} | Any JS object |
array | [1, 2, 3] | Arbitrary JS array |
tuple | (4, 5) | Element, TS new type, fixed length array |
enum | enum{A, B} | Enumeration, new type in TS |
-
literal
let a:10; That gives a a value of 10; We can assign a=10, but we cannot assign a field that is not 10; let b: ‘mail’| ’email’; And then you can assign mail or email to b. But you can’t assign anything else
-
The joint type
let a: number | string;
-
Unknown type Indicates the value of unknown type
When you do not know what type a value is, you can assign the value unknown. It differs from any in that any can be assigned to any variable. Unknown is not allowed
Unknown The method to assign to another variable
- Type judgment
let e: unknown;
e = 10;
let s:string;
if(typeof e === "string"){
s = e;
}
Copy the code
- Types of assertions
// Type assertion, which can be used to tell the parser the actual type of a variable
/* * syntax: * variable as type * < type > variable * * */
s = e as string;
s = <string>e;
Copy the code
- Never: The result is never returned
function error(message: string) :never {
throw new Error(message);
}
Copy the code
- Object represents a JS object
let a: object;
a = {};// Object can be an object
a = function () {};// object can also be function
// {} is used to specify which attributes can be included in the object
{attribute name: attribute value, attribute name: attribute value}
// Add? To the attribute name. , indicating that the property is optional
let b: {name: string, age? : number}; b = {name: Sun Wukong.age: 18};
// [propName: string]: any indicates any type of attribute
let c: {[propName: string]: any};
c = {name: 'Pig Eight Quit'.age: 18.gender: 'male'};
/* * Sets the type declaration of function structures: * syntax: (parameters: type, parameters: type...) => Return value * */
let d: (a: number ,b: number) = >number;
// d = function (n1: string, n2: string): number{
// return 10;
// }
Copy the code
- Array an Array
Array type declaration:
- Type []
- Array < type >
// String [] represents an array of strings
let e: string[];
e = ['a'.'b'.'c'];
// number[] indicates the value
let f: number[];
let g: Array<number>;
g = [1.2.3];
Copy the code
- Tuple a tuple is an array of fixed length
Syntax: [type, type, type]
let h: [string, number];
h = ['hello'.123];
Copy the code
- Enum enumeration
enum Gender{
Male,
Female
}
let i: {name: string, gender: Gender};
i = {
name: Sun Wukong.gender: Gender.Male // 'male'
}
console.log(i.gender === Gender.Male);
Copy the code
- Type the alias
type myType = 1 | 2 | 3 | 4 | 5;// myType is the type alias
let k: myType;
let l: myType;
let m: myType;
Copy the code