Implement a generic GetReadonlyKeys<T>
that returns a union of the readonly keys of an Object.
For example
interface Todo {
readonly title: string
readonly description: string
completed: boolean
}
type Keys = GetReadonlyKeys<Todo> // expected to be "title" | "description"
/* _____________ Your Code Here _____________ */
type GetReadonlyKeys<T> = keyof {
[Key in keyof T as Equal<Pick<T, Key>, Readonly<Pick<T, Key>>> extends true ? Key: never]: T[Key]
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<'title', GetReadonlyKeys<Todo1>>>,
Expect<Equal<'title' | 'description', GetReadonlyKeys<Todo2>>>,
]
interface Todo1 {
readonly title: string
description: string
completed: boolean
}
interface Todo2 {
readonly title: string
readonly description: string
completed?: boolean
}
标签:Typescript,string,_____________,Get,Keys,title,readonly,type,description From: https://www.cnblogs.com/Answer1215/p/16927681.html