Skip to content

Commit

Permalink
switch to better key extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed Sep 17, 2024
1 parent 7b521ff commit 140e5d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions ark/util/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ export const appendUnique = <to extends unknown[]>(
return to
}

export type groupableKeyOf<t> = {
[k in keyof t]: t[k] extends PropertyKey ? k : never
}[keyof t]
export type groupableKeyOf<t> = keyof {
[k in keyof t as t[k] extends PropertyKey ? k : never]: never
}

export type groupBy<element, discriminant extends groupableKeyOf<element>> = {
[k in element[discriminant] & PropertyKey]?: (element extends unknown ?
Expand Down
14 changes: 7 additions & 7 deletions ark/util/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export type toArkKey<o, k extends keyof o> =
export type arkKeyOf<o> =
[o] extends [array] ?
| (number extends o["length"] ? NonNegativeIntegerLiteral : never)
| {
[k in keyof o]-?: k extends `${infer index extends number}` ? index | k
: never
}[keyof o & `${number}`]
: {
[k in keyof o]-?: k extends number ? k | `${k}` : k
}[keyof o]
| keyof {
[k in keyof o as k extends `${infer index extends number}` ? index | k
: never]: never
}
: keyof {
[k in keyof o as k extends number ? k | `${k}` : k]: never
}

export type arkGet<o, k extends arkKeyOf<o>> = o[Extract<
k extends NonNegativeIntegerLiteral ? number : k,
Expand Down
14 changes: 7 additions & 7 deletions ark/util/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ export type keyOf<o> =
number extends o["length"] ?
`${number}`
: keyof o & `${number}`
: {
[k in keyof o]: k extends string ? k
: keyof {
[k in keyof o as k extends string ? k
: k extends number ? `${k}`
: never
}[keyof o]
: never]: never
}

export const keysOf = <o extends object>(o: o): keyOf<o>[] =>
Object.keys(o) as never
Expand Down Expand Up @@ -130,9 +130,9 @@ export const hasDefinedKey: <o extends object, k extends unionKeyOf<o>>(
) => o is extractDefinedKey<o, k> = (o, k): o is any =>
(o as any)[k] !== undefined

export type requiredKeyOf<o> = {
[k in keyof o]-?: o extends { [_ in k]-?: o[k] } ? k : never
}[keyof o]
export type requiredKeyOf<o> = keyof {
[k in keyof o as o extends { [_ in k]-?: o[k] } ? k : never]: never
}

export type optionalKeyOf<o> = Exclude<keyof o, requiredKeyOf<o>>

Expand Down

0 comments on commit 140e5d1

Please sign in to comment.