Skip to content

TypeScript Utility Types API

TypeScript 类型守卫,用于在运行时收窄类型。

1 class · 11 methods

类型守卫

11 methods

在条件分支中收窄类型的运行时检查。

Partial<T>

使用 typeof 运算符收窄原始类型。可识别:'string'、'number'、'boolean'、'bigint'、'symbol'、'undefined'、'object'、'function'。

Parameters

NameTypeDescription
Ttype要收窄的值。

Returns

type

Example

typescript
interface User { id: number; name: string; }
type UserPatch = Partial<User>;
// { id?: number; name?: string }
const patch: UserPatch = { name: 'Alice' };
Required<T>

通过检查原型链收窄为实例类型。

Parameters

NameTypeDescription
Ttype要收窄的值。

Returns

type

Example

typescript
interface Props { a?: number; b?: string; }
type RequiredProps = Required<Props>;
// { a: number; b: string }
Readonly<T>

通过检查属性是否存在来收窄联合类型。

Parameters

NameTypeDescription
keytype要检查的属性名。

Returns

type

Example

typescript
interface Config { url: string; }
type FrozenConfig = Readonly<Config>;
const c: FrozenConfig = { url: '/' };
c.url = '/x';  // Error: readonly
Record<K, V>

用户定义的类型守卫。'x is T' 返回类型谓词在函数返回 true 时触发收窄。

Parameters

NameTypeDescription
Ktype要测试的值。
VtypeValue type.

Returns

type

Example

typescript
type Role = 'admin' | 'user';
const perms: Record<Role, string[]> = {
  admin: ['read', 'write'],
  user: ['read'],
};
Pick<T, K>

Pick only the keys K from T.

Parameters

NameTypeDescription
TtypeSource type.
KtypeKeys to pick (union of keyof T).

Returns

type

Example

typescript
interface User { id: number; name: string; email: string; }
type UserSummary = Pick<User, 'id' | 'name'>;
// { id: number; name: string }
Omit<T, K>

Construct a type with all properties of T except those in K.

Parameters

NameTypeDescription
TtypeSource type.
KtypeKeys to omit.

Returns

type

Example

typescript
interface User { id: number; name: string; email: string; }
type CreateUserDTO = Omit<User, 'id'>;
// { name: string; email: string }
Exclude<T, U>

Exclude from union T all types assignable to U.

Parameters

NameTypeDescription
TtypeSource union.
UtypeTypes to exclude.

Returns

type

Example

typescript
type T = string | number | boolean;
type N = Exclude<T, boolean>;
// string | number
Extract<T, U>

Extract from union T all types assignable to U.

Parameters

NameTypeDescription
TtypeSource union.
UtypeTypes to extract.

Returns

type

Example

typescript
type T = string | number | boolean;
type S = Extract<T, string>;
// string
NonNullable<T>

Exclude null and undefined from T.

Parameters

NameTypeDescription
TtypeSource type.

Returns

type

Example

typescript
type T = string | null | undefined;
type NN = NonNullable<T>;
// string
ReturnType<T>

Return the return type of function type T.

Parameters

NameTypeDescription
TtypeFunction type.

Returns

type

Example

typescript
function getUser() { return { id: 1, name: 'a' }; }
type User = ReturnType<typeof getUser>;
// { id: number; name: string }
Parameters<T>

Return a tuple type of the parameter types of function type T.

Parameters

NameTypeDescription
TtypeFunction type.

Returns

type

Example

typescript
function add(a: number, b: number) { return a + b; }
type AddArgs = Parameters<typeof add>;
// [number, number]