-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript tricks #19
Comments
interface 与 type 区别
|
tsconfig.jsonnpx tsc --init 生成 tsconfig.json |
遇到问题汇总1. Element 类型不存在 style属性使用 HTMLElement 即可。 |
||= ,&&=,??=看开源项目时,看到
例如我们对 对象某个属性添加元素时,要先判断是否是数组。利用
除了 let b: Record<string, undefined | any[] | number> = {
}
b.a ||= [] // b.a || b.a = []
b.c &&= [] // b.c && b.c = []
b.d ??= [] // b.d !== null && b.d !== undefined ? b.d : [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Utility Types
Omit
Omit
可以忽略对象类型
中的某些属性等同于
自身实现,利用Exclude 剔除属性
Pick
Pick
的功能和Omit
相反,Pick
筛选出对象类型
中需要的属性等同于
本身的实现也很简单
Exclude
Omit
的实现有用到 Exclude, 它的作用就是将第一个类型中,符合第二个类型中条件的类型剔除对于 对象类型等,第二个类型是第一个类型的子集也可以达到效果
自身实现
Extract
Extract
的功能和Exclude
相反, 它的作用就是将第一个类型中,符合第二个类型筛选的类型保留自身实现
Partial
Partial
可以让对象类型
的每个属性变为可选等同于
自身实现
Required
Required
可以让对象类型
的的每个属性都变为必须等同于
Required
的实现 使用了-?
,语法奇怪但是很好理解Record
Record
可以定义对象类型
键和值的类型自身实现
ReturnType
ReturnType
可以返回函数类型
的返回值类型等同于
自身实现
Parameters
Parameters
可以返回函数类型
的参数类型等同于
它的实现也是通过
infer
来进行实现Awaited
对于
async
函数或者返回一个promise
值时, 我们可以通过Awaited
得到未被Promise
包裹的类型The text was updated successfully, but these errors were encountered: