掘金整理较全的地址:
https://juejin.cn/post/6844904182843965453#heading-42
gitbook:
https://jkchao.github.io/typescript-book-chinese
1. typeof 有什么作用?
使用 typeof 获取值所对应的类型
var a = 1;type b = typeof a;
2. keyof 有什么作用?
获取类型中 Key 的联合类型
var person = {name: 'alex',age: 30}type Person = keyof typeof person; //这里的 Person 为 'name' | 'age'interface IPerson {name: 'alex',age: 30}type KPerson = keyof Person; // 这里 KPerson 为 'name' | 'age'
3. TS 中类型保护的场景是什么?
在 ts 的条件块中如果出现 typeof,instanceof,in,字面量类型 的判断时,在 if 的代码块中会进行类型推断
当判断对象为普通对象,无法使用 typeof,instanceof 等判断时,可以自定义类型保护函数 isBar
interface Bar {name: 'alex'}function isBar (val: any): val is Bar {return true;}function myName (val: any) {if (isBar(val)) {console.log(`my name is ${val.name}`)}}myName({ age: 30 })
4. 查看以下代码,说出 ret 是什么类型?
function arrayToMap<T extends number> (arr: T[]): { [K in T]: K } {return arr.reduce((res, key) => res[key] = key, Object.create(null));}const ret = arrayToMap([1, 2, 3]);
ret 是一个对象类型,对象中的每一个属性为字面量类型
5. readonly 有哪些应用场景?
类属性,函数参数,类型属性,接口属性,或者是数组
var aa: readonly number[] = [1, 2, 3];aa[0] = 4 //报错
6. 内置 API Readonly 的作用是什么?
把一个类型的所有属性都变成只读
interface Person {name: string;age: number;}type ReadonlePerson = Readonly<Person>;
7. 自定义 Readonly 的实现?
type Readonly<T extends object> = {readonly [K in keyof T]: T[K]}
这里有个疑问 🤔️:为什么用 interface 就报错呢?
interface Readonly<T> {[K in keyof T]: T[K]}
8. 数组每项如何转为只读
var a: ReadonlyArray<number> = [1, 2, 3];a[0] = 4; // 这里会报错,提示只读
自定义实现 ReadonlyArray
type MyReadonlyArray<T> = readonly T[];type a = MyReadonlyArray<number>;// a 的类型为 readonly number[]
9. 使用泛型实现数组反转
function reverse<T> (arr: T[]): T[] {let ret: T[] = [];arr.forEach(item => {ret.unshift(item)})return ret;}console.log(reverse([1, 2, 3]))console.log(reverse(['4', '5', '6']))console.log(reverse([Symbol(7), Symbol(8), Symbol(9)]))
10. 泛型约束是什么?
在使用泛型的过程中,泛型表示可随便传任何类型的值,所谓的约束即 extends string
type OnlyString<T extends string> = {name: T}type a = OnlyString<string>
11. 使用泛型定义一个查询用户接口
import axios, { AxiosResponse } from 'axios'interface Response<T> {code: number;message: string;body: T,state: number;}function getUser<T> (userId: string): Promise<AxiosResponse<Response<T>>> {return axios.get<Response<T>>('/user/' + userId)}interface User {name: string;age: number;}const promise = getUser<User>('123')promise.then(res => {res.data.body.name})
12. 怎么时候会走到 never 类型?
- 抛异常的函数,返回值为 never
- 不可能走到的分支代码
13. TS 如何判断两个类型是否相等?
只能通过判断类型 T 是否 extends 至类型 K,不能用于 === 判断
14. 如何从一个对象中提取出部分属性作为一个新类型
比如 Person 有 name 和 age,提取一个类型,只有 name 属性
interface Person {name: string;age: number;school: string;}interface Man {name: string;age: number;}type MyName = Extract<keyof Person, keyof Man>type NameType = Pick<Person, MyName>
15. TS 如何进行详细的检查
比如像 ast 枚举词法时,如何保证词法穷举,可以使用一个走到会报错的方法来实现
const _exhaustiveCheck: never = param;
