-
-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
KnowKeys #164
Comments
Approved. This can benefit all the export type _KnownKeys<O extends object> = {
[K in keyof O]-?: string extends K
? never
: number extends K
? never
: K
} extends {
[K in keyof O]: infer K
} ? K & keyof O : never And we could rewrite other utilities such as: export type _RequiredKeys<O extends object> = {
[K in keyof O]-?: string extends K
? never
: number extends K
? never
: K
} extends {
[K in keyof O]: infer K
} ? K & keyof O : never Thanks! |
yes i had to use it with SelectKeys in order to work properly |
Would you mind posting an example? |
i'm using an ORM so i created export type PrimaryID<T> = Any.Type<T, 'PrimaryID'> & ID<T>
type KnownKeys<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K
} extends { [_ in keyof T]: infer U }
? U
: never
export type OnlyDeclared<T> = Pick<T, KnownKeys<T>>
export type GetPrimaryID<T extends object> = T[Object.SelectKeys<
OnlyDeclared<T>,
PrimaryID<{}>
>]
export type TypeOfPrimaryID<T extends object> = GetPrimaryID<T> extends PrimaryID<infer X>
? X
: never
export class User {
id: PrimaryID<string>
name: string
age: number
phones: Phone[]
[prop: string]: unknown
constructor(data: Partial<User>) {
super(data)
}
}
function findById(id: TypeOfPrimaryID<User>) {} |
馃崺 Feature Request
Is your feature request related to a problem?
To get only know keys of an object
Describe the solution you'd like
https://stackoverflow.com/a/51956054/8741524
The text was updated successfully, but these errors were encountered: