keyof 操作符
keyof 是类型运算符。keyof 后面跟着的是复杂类型,返回值是类型,复杂类型的属性字面量联合类型。
简单理解:接收类型,返回类型。返回接收类型的属性字面量联合类型。
class Product {
name: string;
price: number;
};
// 返回属性字面量联合类型 name | price
type Productkeys = keyof Product;
const k1: Productkeys = "price";
const k2: Productkeys = "name";
// const k2: Productkeys = "a"; // 不能将类型"a"分配给类型 "keyof Product"
class Product {
name: string;
price: number;
};
class Store<T> {
protected _objects: T[] = [];
add(obj: T) {
this._objects.push(obj);
};
// 根据属性及属性值在 _objects 中查找对象
find(key: keyof T, value: unknown): T | undefined {
return this._objects.find(item => item[key] === value);
};
};
const store = new Store<Product>();
store.add({name: "张三", price: 100 });
// 查找
store.find("name", "张三");
映射类型
基于现有类型产生新的类型,通过遍历语法将原有类型进行拷贝,再在拷贝的基础上进行修改。
接收类型,返回类型,返回的类型中所有的属性都是只读的。
interface Product {
name: string;
price: number;
};
// 将T类型中的属性变为只读
type MyReadonly<T> = {
readonly [K in keyof T]: T[K];
};
const product: MyReadonly<Product> = {
name: "张三",
price: 100,
};
// product.name = "李四"; // 已经变为只读,无法修改
接收类型,返回类型,返回的类型中所有的属性都是可选的。
interface Product {
name: string;
price: number;
};
// 将T类型中的属性都是可选的
type Optional<T> = {
[K in keyof T]? = T<K>;
};
const product: Optional<Product> = {
name: "张三",
price: 100,
};
接收类型,返回类型,返回的类型中所有的属性都是可写的。
interface Product {
readonly name: string;
readonly price: number;
};
// 将T类型中的属性都是可写的
type Writable<T> = {
-readonly [K in keyof T] = T<K>;
};
const product: Writable<Product> = {
name: "张三",
price: 100,
};
接收类型,返回类型,返回的类型中所有的属性都是必选的。
interface Product {
name?: string;
price?: number;
};
// 将T类型中的属性都是可写的
type Necessary<T> = {
[K in keyof T]-? = T<K>;
};
const product: Necessary<Product> = {
name: "张三",
price: 100,
};
工具类型
官网文档:https://www.typescriptlang.org/docs/handbook/utility-types.html
Partial
将类型中的属性都变成可选的,接收类型,返回类型。
interface Person {
name: string;
age: number;
};
// { name?: string, age?: number };
const person: Partial<Person> = {
name: "张三",
age: 18
};
Readonly
将类型中的属性都变成只读的,接收类型,返回类型。
interface Person {
name: string;
age: number;
};
// { readonly name: string, readonly age: number };
const person: Readonly<Person> = {
name: "张三",
age: 18
};
Recore
用于约束字典类型。
// 普通字典约束方式
const list: { [id: number]: { id: number; title: string } } = {
1: { id: 1, title: "···" },
2: { id: 2, title: "···" },
};
// 语法格式
Recore<属性类型, 值类型> = {}
// Recore约束字典
interface Port {
id: number;
title: string;
};
const list: Recore<number, Post> = {
1: { id: 1, title: "···" },
2: { id: 2, title: "···" },
};
Omit
接收类型,得到新类型,在新类型中不要包含keys。
简单理解:在现有的类型中过滤一些属性,从而得到新的类型。
interface Todo {
title: string;
description: string;
completed: boolean;
createdAt: number;
};
// { title: string, description: string }
type TodoPreview = Omit(Todo, "completed" | "createdAt");
const todo: TodoPreview = {
title: "test",
description: "test"
};
Pick
接收类型,返回新类型,在新类型中要包含 keys。
简单理解:在现有类型中保留某些属性,从而获得新的类型。和 Omit 相反。
interface Todo {
title: string;
description: string;
completed: boolean;
createdAt: number;
};
// { title: string, completed: boolean }
type TodoPreview = Pick(Todo, "title" | "completed");
const todo: TodoPreview = {
title: "test",
completed: false,
};
Exclude
接收联合类型,得到新类型,在新类型中排除联合类型中的某一个类型或多个类型。
// 语法格式
Exclude<联合类型, 要排除的类型>;
type T0 = Exclude<"a" | "b" | "c", "c">; // "a" | "b"
is
用于加强 TypeScript 类型推断,可以缩小类型的范围。
// 判断x是否为字符串,是字符串将它变为大写
function toUpperCase(x: any) {
if (isString(x)) {
return x.toUpperCase();
};
};
// 判断参数是否为字符串的函数
// value is string:如果函数返回值为 true, value 参数的类型就是字符串
function isString(value: any): value is string {
return typeof value === "string";
};