@ fringeworks/ 类型
@fringeworks/types 是一个库, 某些将找到方便, 提供 TypeScript 工具类型和常数, 以列出一个类型的值 .
安装
npm install @fringeworks/types
类型和常数
数值类型
| 类型 | 定义 | 说明 |
|---|---|---|
Nullish | undefined | null | 无效或未定义 |
Primitive | Nullish | boolean | number | bigint | string | symbol | 原始价值 |
Empty | Nullish | '' | 无效加空字符串 |
Falsy | Empty | false | 0 | 0n | JavaScript falsy 值( 不包括) NaN(中文(简体) ) |
Falsy 不包含 NaN,因为 NaN 没有字面类型,只能通过包括所有 number。 。 。 . -0 与 0 在类型级别.
常数
列出某一类型可能值的常数。 也导出同名类型,因此每个类型都可以作为值和类型.
| 常数 | 说明 |
|---|---|
ToStringResult | 结果 Object.prototype.toString.call(value) 主要内置对象 |
TypeOfResult | 结果 typeof |
import { ToStringResult, TypeOfResult } from '@fringeworks/types';
// Use as a value
Object.prototype.toString.call([]) === ToStringResult.Array; // => true
// Use as a type
function getType(value: unknown): TypeOfResult {
return typeof value;
}
记录类型
键为 PropertyKey (单位:千美元)string | number | symbol) (中文(简体) ). 指定值类型, 使用 Record<PropertyKey, VALUE> 直接说.
| 类型 | 定义 | 说明 |
|---|---|---|
LooseRecord | Record<PropertyKey, any> | 接受任何值类型的灵活性第一类型 |
StrictRecord | Record<PropertyKey, unknown> | 需要类型守护的安全第一类型 |
词典类型
键为 string 只有这个 指定值类型, 使用 Record<string, VALUE> 直接说.
| 类型 | 定义 | 说明 |
|---|---|---|
LooseDictionary | Record<string, any> | 接受任何值类型的灵活性第一类型 |
StrictDictionary | Record<string, unknown> | 需要类型守护的安全第一类型 |
阵列类型
| 类型 | 定义 | 说明 |
|---|---|---|
LooseArray | any[] | 接受任何元素的弹性第一阵列类型 |
StrictArray | unknown[] | 需要使用类型守护的安全第一阵列类型 |
同步函数类型
| 类型 | 说明 |
|---|---|
SyncFunction<PARAMS, RESULT> | 有可配置参数类型的基准类型 PARAMS (tuple,默认) never[]返回类型 RESULT (默认) unknown(中文(简体) ) |
LooseSyncFunction | 灵活性第一类型 any 参数和返回值 |
StrictSyncFunction | 安全第一类型 never[] 参数和 unknown 返回值。 与 SyncFunction 默认值 |
同步函数类型
| 类型 | 说明 |
|---|---|
AsyncFunction<PARAMS, RESULT> | 有可配置参数类型的基准类型 PARAMS (tuple,默认) never[]返回时 Promise<RESULT> (单位:千美元)RESULT 默认为 unknown(中文(简体) ) |
LooseAsyncFunction | 灵活性第一类型 any 参数和返回值 |
StrictAsyncFunction | 安全第一类型 never[] 参数和 Promise<unknown> 返回值。 与 AsyncFunction 默认值 |
斜纹类型
| 类型 | 签署 | 说明 |
|---|---|---|
IndexedIteratee<VALUE, RESULT> | (value: VALUE, index: number, data: readonly VALUE[]) => RESULT | 回呼在阵列上延展 |
FilterFunction<VALUE> | (value: VALUE) => boolean | 显示元素是否匹配条件的函数 。 可以传到 Array.prototype.filter |
SortFunction<VALUE> | (a: VALUE, b: VALUE) => number | 比较函数返回两个元素的顺序(负置出) a (第一编) 可以传到 Array.prototype.sort |
颜色类型
| 类型 | 定义 | 说明 |
|---|---|---|
AbsoluteColor | HexColor | FunctionalColor | NamedColor | 混凝土颜色值:十六进制颜色、颜色函数或颜色名称 |
HexColor | `#${string}` | 十六进制颜色 |
FunctionalColor | `rgb(${string})` | | `color(${string})` | 带有色彩功能的颜色 |
NamedColor | 'aliceblue' | | 'yellowgreen' | 'transparent' | CSS 颜色名称(148个名称和 transparent(中文(简体) ) |
AbsoluteColor 用于在 JavaScript 中计算颜色的代码。 在渲染之前尚未决定的值,例如 CSS 变量(var(), (中文(简体) ), currentColor,以及 inherit,是类型错误. FunctionalColor 接受颜色函数 rgb(), (中文(简体) ), rgba(), (中文(简体) ), hsl(), (中文(简体) ), hsla(), (中文(简体) ), hwb(), (中文(简体) ), lab(), (中文(简体) ), lch(), (中文(简体) ), oklab(), (中文(简体) ), oklch(),以及 color() (无) color-mix() 和类似.
类型只检查语法,而不是内容(例如 '#zzz') (中文(简体) ). 颜色名称和函数名称只在小写中被接受.
import type { AbsoluteColor } from '@fringeworks/types';
const accent: AbsoluteColor = '#2f6fde'; // OK
const brand: AbsoluteColor = 'var(--brand)'; // Type error
高级公用事业类型
Keys<RECORD, TYPE>
生成记录键的结合类型。 何时 TYPE ,只包含其值延伸该类型的密钥.
可选属性按其值类型检查,而不包含 undefined 由 ?。 。 。 .
import type { Keys } from '@fringeworks/types';
type Props = {
id: number;
name: string;
label: string;
createdAt: Date;
note?: string;
};
// Union of all keys
type AllKeys = Keys<Props>;
// => 'id' | 'name' | 'label' | 'createdAt' | 'note'
// Only keys whose value is string
type StringKeys = Keys<Props, string>;
// => 'name' | 'label' | 'note'
Prefixed<PREFIX, RECORD, KEYS>
通过在记录的指定属性上预置骆驼Case前缀来生成新类型.
何时 KEYS ,所有属性都成为目标。 数字和符号键按原样保存,没有前缀.
import type { Prefixed } from '@fringeworks/types';
type Props = {
width: number;
height: number;
color: string;
};
// Prefix all properties
type PrefixedProps = Prefixed<'default', Props>;
// => { defaultWidth: number; defaultHeight: number; defaultColor: string }
// Prefix only selected properties
type PartialPrefixedProps = Prefixed<'default', Props, 'width' | 'height'>;
// => { defaultWidth: number; defaultHeight: number; color: string }
PrefixedUnion<PREFIX, UNION, MEMBERS>
使用骆驼来创造新的工会类型 工会成员的案件前缀.
何时 MEMBERS 所有成员都成为目标。 非弦成员保持原样,没有前缀.
import type { PrefixedUnion } from '@fringeworks/types';
type Direction = 'top' | 'right' | 'bottom' | 'left';
// Prefix all members
type PaddingKeys = PrefixedUnion<'padding', Direction>;
// => 'paddingTop' | 'paddingRight' | 'paddingBottom' | 'paddingLeft'
// Prefix only selected members
type PartialMarginKeys = PrefixedUnion<'margin', Direction, 'top' | 'bottom'>;
// => 'marginTop' | 'right' | 'marginBottom' | 'left'
Override<BASE, OVERRIDES>
生成属性为 BASE 被弃绝者 OVERRIDES。同名属性从 OVERRIDES。 。 。 .
import type { Override } from '@fringeworks/types';
type Base = { id: number; name: string };
type Props = Override<Base, { id: string; label: string }>;
// => { name: string } & { id: string; label: string }
许可证
麻省理工学院