关键字(18 个)

Keyword Description Example
**<font style="color:#117CEE;">let</font>** 声明一个块作用域变量 <font style="color:rgb(20, 20, 20);">let age: number = 25;</font>
**<font style="color:#117CEE;">const</font>** 声明一个块作用域的常量 <font style="color:rgb(20, 20, 20);">const PI: number = 3.14;</font>
**<font style="color:#117CEE;">var</font>** 声明函数作用域或全局作用域的变量 <font style="color:rgb(20, 20, 20);">var name: string = "John";</font>
**<font style="color:#117CEE;">function</font>** 定义一个函数 <font style="color:rgb(20, 20, 20);">function greet(name: string): string {</font>
<font style="color:rgb(20, 20, 20);">return 'Hello ' + name;</font>
<font style="color:rgb(20, 20, 20);">}</font>
**<font style="color:#117CEE;">class</font>** 定义一个类 <font style="color:rgb(20, 20, 20);">class Person { name: string; }</font>
**<font style="color:#117CEE;">interface</font>** 声明自定义类型 <font style="color:rgb(20, 20, 20);">interface IUser { name: string; }</font>
**<font style="color:#117CEE;">type</font>** 声明类型别名 <font style="color:rgb(20, 20, 20);">type Point = { x: number; y: number; };</font>
**<font style="color:#117CEE;">enum</font>** 声明一个枚举 <font style="color:rgb(20, 20, 20);">enum Color { Red, Green, Blue }</font>
**<font style="color:#117CEE;">public</font>** 指定公共成员访问权限 <font style="color:rgb(20, 20, 20);">public name: string;</font>
**<font style="color:#117CEE;">private</font>** 指定私有成员访问权限 <font style="color:rgb(20, 20, 20);">private age: number;</font>
**<font style="color:#117CEE;">protected</font>** 指定受保护的成员访问权限 <font style="color:rgb(20, 20, 20);">protected type: string;</font>
**<font style="color:#117CEE;">async</font>** 将函数标记为异步 <font style="color:rgb(20, 20, 20);">async function fetchData() { }</font>
**<font style="color:#117CEE;">await</font>** 暂停异步函数的执行,直到 promise 完成 <font style="color:rgb(20, 20, 20);">let data = await fetch(url);</font>
**<font style="color:#117CEE;">null</font>** 表示没有任何值 <font style="color:rgb(20, 20, 20);">let empty: null = null;</font>
**<font style="color:#117CEE;">undefined</font>** 指示变量的未初始化状态 <font style="color:rgb(20, 20, 20);">let notAssigned: undefined;</font>
**<font style="color:#117CEE;">any</font>** 允许任何类型,绕过编译器检查 <font style="color:rgb(20, 20, 20);">let flexible: any = 5;</font>
**<font style="color:#117CEE;">unknown</font>** any的类型安全对应类型 <font style="color:rgb(20, 20, 20);">let mysterious: unknown;</font>
**<font style="color:#117CEE;">never</font>** 表示永远不会出现的值 <font style="color:rgb(20, 20, 20);">function error(): never {</font>
<font style="color:rgb(20, 20, 20);">throw new Error();</font>
<font style="color:rgb(20, 20, 20);">}</font>

关键符号

Keyword Description Example
readonly
Readonly
声明只读属性
也可以使用只读泛型接口包裹
const arr: readonly number[] = [0, 1];
type t = Readonly<[number, number]> 只读元组
typeof 类型运算符 const a = { x: 0 };
type T0 = typeof a; // { x: number }
? 可选运算符 let a: [number, number?] = [1];
扩展运算符
右边例子表示不限成员数量的元组
type myTuple = [
number,
…string[]
];
unique 唯一的,表示某类型变量是不能修改值的 const x: unique symbol = Symbol();
static 静态修饰符 class C {
static readonly foo: unique symbol = Symbol();
}
as 类型断言 const arr = [1, 2] as const
void 空类型 const hello: (txt: string) => void = function(txt) {
console.log(‘hello ‘ + txt);
};
delete 删除属性 const myUser = {
name: “Sabrina”
};

delete myUser.name // 虽然报错,因为不能删除对象属性
?? Null 判断运算符 let firstName = user.firstName ?? ‘Foo’;
declare 该命令表示某变量的具体定义 interface Circle {
area: bigint;
}

interface Rectangle {
area: number;
}

declare const s: Circle
Rectangle;
s.area; // bigint
number
keyof 属性映射 interface Point {
x: number;
y: number;
}

type PointCopy1 = {
[Key in keyof Point]: Point[key];
};
非空断言 class Point {
x!: number;
y!: number;
}
instanceof 实例相等判断,不是类型判断 obj instanceof Person // false 这里的 obj 是 object,而 Person 是 class