2023-12-05-Typescript学习

  1. 为对象赋值未定义的变量
1
2
3
4
5
6
interface IObject {
a: string;
[b: string]: any;
readonly c: string;
}
var obj: IObject = { a: "1", b: 2, c: "4", d: true };
  1. 联合类型函数重载
1
2
3
4
5
6
7
function func(value: number): number;
function func(value: string): string;
function func(value: number | string): number | string {
return value;
}
let a: number = func(1);
let b: string = func("1");
  1. 类型断言,只能断言联合类型中的类型
1
2
3
4
5
function getAssert(name: string | number) {
// return (<string>name).length;
// jsx中必须使用 as,<>有解析问题
return (name as string).length;
}
  1. 类型别名 type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
type Type0 = string | number;
type Sex = "男" | "女";
interface IType1 {
name: string;
}
interface IType2 {
age: string;
}
function getSex(sex: Sex) {
return sex;
}
type Type3 = IType1 | IType2;
var a: Type3 = { name: "张三" };
var b: Type3 = { age: 18 };
var b: Type3 = { name: "李四", age: 12 };
getSex("男");
  1. 枚举,会被编译为一个双向映射的对象
1
2
3
4
5
6
7
8
9
enum Days {
SUN,
MON,
WED,
THU,
WED,
FRI,
SAT,
}
  1. 类的修饰符,public prvate protected(仅子类可访问),static
1
2
3
4
class Person {
name = "张三";
protected age = 18;
}
  1. 泛型,帮助限定约束规范
1
2
3
4
5
6
7
8
9
// 方法中的泛型
function createArray<T>(len: number, value: T): Array<T> {
let arr = [];
for (var i = 0; i < len; i++) {
arr[i] = value;
}
return arr;
}
// 接口中的泛型
  1. 元组
1
const t1: [string, number, string] = ["1", 2, "3"];

2023-12-05-Typescript学习
https://zhangyingxuan.github.io/2023-12-05-Typescript学习/
作者
blowsysun
许可协议