#Constraint for object key

6 messages · Page 1 of 1 (latest)

idle pawnBOT
#

@neon oracle Here's a shortened URL of your playground link! You can remove the full link from your message.

evan.trimboli#6544

Preview:```ts
type RowType = "string" | "number" | "boolean"

export type MainConfig<TKey extends string> = Readonly<
Record<TKey, RowType>

export const objectKeys = <T extends object>(
obj: T
): readonly (keyof T)[] =>
Object.keys(obj) as (keyof T)[]

const go = <
TKey extends string,
TMainConfig extends MainConfig<TKey>

(
cfg: TMainConfig
...```

fierce charm
#

It really depends on what your function is actually doing.

#

Without knowing that, my best answer would be to just remove the key generic.

idle pawnBOT
#
Burrito#6903

Preview:```ts
type RowType = "string" | "number" | "boolean"

export type MainConfig<TKey extends string> = Readonly<
Record<TKey, RowType>

export const objectKeys = <T extends object>(
obj: T
): readonly (keyof T)[] =>
Object.keys(obj) as (keyof T)[]

const go = <TMainConfig extends MainConfig<string>>(
cfg: TMainConf
...```

thick cedar
#

It can work if you pass your generic TKey to the objectKeys function