sindresorhus/type-fest

Support variables in Get paths

オープン

#567 opened on 2023/03/07

 (3 件のコメント) (5 件のリアクション) (0 人の担当者)TypeScript (471 件のフォーク)batch import
component:pathsenhancementhelp wanted

Repository metrics

Stars
 (12,328 個のスター)
PR merge metrics
 (平均マージ 3d 5h) (30d で 11 merged PRs)

説明

It would be nice if type-fest could support variables in paths passed to Get.

Example:

const obj = { array: [{ a: 1 }, { a: 2 }] } as const

const n = 0 as number
const get = <P extends string>(a: P) => a as Get<typeof obj, P>

const t1 = get(`array[10].a`) // 1 | 2 | undefined
const t2 = get(`array.10.a`) // 1 | 2 | undefined

const t3 = get(`array[${n}].a`) // unknown
const t4 = get(`array.${n}.a`) // unknown

Passing an index that may not exist in the array already works today (t1 and t2). It would be nice if this could be extended to array and object lookup with variables, so that t3 and t4 would get the same types.

Is this feature something you think would be good to have in this library?

コントリビューターガイド