1. Use literals for type declarations, and they cannot be modified after declarations
let a: 10;
// a = 10 可以 a = 11 不可以
2. You can use | to connect multiple types (joint types)
let c: boolean | string;
// c = true; c = 'hha'; 可以 a = 123 不可以
3.any any type
一个变量设置类型为any后相当于对该变量关闭了TS类型检测
使用ts时,不建议使用any
let d; 声明变量不指定类型,TS解析器会自动判断变量的类型为any
4.unknown represents an unknown type of value
let e: unknown;
// unknown类型的值赋值给别的类型的会报错
5. Type assertion, you can tell the actual type of the parser variable
d = e as string;
Or write like this, tell the variable e is a string
d = <string>e;
6.viod is used to indicate empty, take function as an example, it means a function without return value
function fn(): void{
// return 就报错
}
7.never means that the result will never be returned, it can be used like this
function fn1(): never{
throw new Error('报错了')
}
8. Object, {} is used to specify which attributes can be included in the object
let obj2 : {
name : string}
obj2 = {
name : '孙悟空'}
Add after the attribute name? , Indicating that the attribute is optional
let obj2 : {
name : string , age?:number};
obj2 = {
name : '孙悟空',age:18}
[propName:string]:any represents any type of attribute,
as long as there is a name attribute in obj3, you can add other attributes at will
let obj3: {
name:string,[propName:string]:any};
obj3 = {
name:'哈哈',age:18,name2:'james'}
9. Arrow functions
const gg = (a:number,b:number) :number =>{
return 1
}
10. Array
let arr1: string[]; //字符串数组
let arr2: number[]; //表示数值数组;
11. Tuples, which are fixed-length arrays
let yz: [string,string];
yz = ['hee','abc'];
12. Enumeration
enum Gender{
Male = 0,
Female = 1
}
let i: {
name:string,gender:Gender};
i = {
name:'孙悟空',
gender:Gender.Male
}
console.log(i.gender === Gender.Male) //true
13.& means at the same time
let jj: {
name:string} & {
age:number};
jj = {
name:'孙悟空',age:18}