@saco/types
v1.0.3
Published
Global TypeScript utility types for saco
Readme
@saco/types
全局 TypeScript 工具类型。安装后无需 import,直接使用。
本文档由 npm run build 根据 src/*.d.ts 注释生成,请勿手改。
安装
npm i @saco/typestsconfig.json:
{
"compilerOptions": {
"types": ["@saco/types"]
}
}已有 types 数组时,追加 "@saco/types" 即可。
类型
AnyObj
任意键值的对象
例如:
const obj: Record<string, any> = { a: 'x' }
任意:
const obj: AnyObj = { a: 'x' }
const obj: AnyObj<number> = { a: 1 }any-obj.d.ts
HTMLAttributes
允许任意 data-*
若在 vue 定义,需与 tsconfig vueCompilerOptions.dataAttributes 同步
interface HTMLAttributeshtml.d.ts
InputHTMLAttributes
选文件夹上传(非标准属性,DOM 有、Vue 类型未收录)
interface InputHTMLAttributeshtml.d.ts
Intersection
交集
A = { a: string, b: number }
B = { a: number, b: number, c: boolean }
Intersection<A, B> => { a: string, b: number } | { a: number, b: number }intersection.d.ts
Joint
联合类型:将多个类型联合起来,形成一个新的类型
Joint<[A, B]> => A | Bjoint.d.ts
Keys
获取对象键名(排除数字和符号),得到键名数组
例如:
const obj = { a: '1' }
Object.keys(obj).forEach(key => {
const item = obj[key as keyof typeof obj]
})
获取后:
(Object.keys(obj) as Keys<typeof obj>).forEach(key => {
const item = obj[key]
})keys.d.ts
pdfjs-dist
兼容pdfjs-dist提示
declare module 'pdfjs-dist'pdfjs-dist.d.ts
Range
生成 0 到 N-1 的数字联合(不是元组,也不含 N)
Range<5> => 0 | 1 | 2 | 3 | 4range.d.ts
SetOptional
将对象指定键设为可选(K 为 T 的键)
例如:
type A = { a: string; b: number };
可选后:
type Rust = SetOptional<A, 'a'>;
鼠标悬停 Rust 显示:{ a?: string; b: number }set-optional.d.ts
SetRequired
将对象指定键设为必选(K 为 T 的键)
例如:
type A = { a?: string; b?: number };
必选后:
type Rust = SetRequired<A, 'a'>;
鼠标悬停 Rust 显示:{ a: string; b?: number }set-required.d.ts
Simplify
简化类型
例如:
type A = { a: string };
type B = { b: number };
type Rust = A & B & { c: boolean };
鼠标悬停 Rust 显示:A & B & { c: boolean }
简化:
type Rust = Simplify<A & B & { c: boolean }>
鼠标悬停 Rust 显示:{ a: string, b: number, c: boolean }simplify.d.ts
