Skip to content

ReadonlyKeys ​

自 1.0.0 起

获取只读属性的键。

签名 ​

typescript
type ReadonlyKeys<T> = {
  [K in keyof T]-?: IfEquals<
    { [Q in K]: T[K] },
    { -readonly [Q in K]: T[K] },
    never,
    K
  >
}[keyof T]

参数 ​

参数描述
T对象类型

描述 ​

从对象类型中提取所有只读属性的键。

示例 ​

基本用法 ​

typescript
import type { ReadonlyKeys } from 'uni-types'

interface User {
  id: number
  name: string
  readonly email: string
  readonly createdAt: Date
}

type Readonly = ReadonlyKeys<User>
// 'email' | 'createdAt'

全部可写 ​

typescript
interface WritableUser {
  id: number
  name: string
}

type NoReadonly = ReadonlyKeys<WritableUser> // never

相关 ​

基于 MIT 许可发布