Skip to content
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

Closed
mdbetancourt opened this issue Dec 14, 2020 · 4 comments
Closed

KnowKeys #164

mdbetancourt opened this issue Dec 14, 2020 · 4 comments
Labels
enhancement Improving something feature Add new features
Projects

Comments

@mdbetancourt
Copy link

mdbetancourt commented Dec 14, 2020

馃崺 Feature Request

Is your feature request related to a problem?

To get only know keys of an object

Describe the solution you'd like

type KnownKeys<T> = {
  [K in keyof T]: string extends K ? never : number extends K ? never : K
} extends { [_ in keyof T]: infer U } ? U : never;

https://stackoverflow.com/a/51956054/8741524

@millsp
Copy link
Owner

millsp commented Dec 14, 2020

Approved. This can benefit all the ...Keys utilities too, since they return never in the case outlined on StackOverflow.

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!

@millsp millsp added enhancement Improving something feature Add new features labels Dec 14, 2020
@mdbetancourt
Copy link
Author

Approved. This can benefit all the ...Keys utilities too, since they return never in the case outlined on StackOverflow.

yes i had to use it with SelectKeys in order to work properly

@millsp
Copy link
Owner

millsp commented Dec 14, 2020

Would you mind posting an example?

@mdbetancourt
Copy link
Author

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>) {}

@millsp millsp added this to To do in Board via automation Jan 29, 2021
@millsp millsp moved this from To do to Done in Board Jan 30, 2021
@millsp millsp closed this as completed Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving something feature Add new features
Projects
Board
  
Done
Development

No branches or pull requests

2 participants