Typescript - keyof object as 'OR strings option'

Typescript - keyof object as 'OR strings option'

我正在导入一个 .json 文件,我想获取所有键作为可选参数

import jsonFile from '../public/sample.json'

json 有未知键,但对于这个问题,假设它有以下内容:

{
  "greetings": {
    "hello": "Hello",
        "hello_informal": "Hi",
        "good_morning": "Good morning",
        "good_afternoon": "Good afternoon",
        "good_night": "Good night",
        "bye": "Bye"
  },
  "commons": {
    "pagination": {
            "first_page": "First page",
            "last_page": "Last page",
            "next_page": "Next page",
            "previous_page": "Previous page"
        }
  },
  "home": "Home sweet home"
}

我有一个带 1 个参数的函数。

function sample(keyValue: TypeKeyValue) {
  // logic here...
  console.log("Thank you Whosebug", keyValue)
}

我的问题是如何为所有对象键(包括嵌套键,以带点的父键作为前缀)的 keyValue 参数创建类型。 键的值无关紧要,只需将所有键收集为可选即可。

我正在尝试这个,但没有按预期工作:

type TypeKeyValue: keyof jsonFile 

期望的结果应该是一种自动获取以下内容的方法:

type TypeKeyValue: "greetings" | "greetings.hello" | "greetings.hello_informal" | "greetings.good_morning" | "greetings.good_afternoon" | "greetings.good_night" | "greetings | bye" | "commons" | "commons.pagination" | "commons.pagination.first_page" | "commons.pagination.last_page" | "commons.pagination.next_page" | "commons.pagination.previous_page" | "home"

任何帮助或解决方案将不胜感激

只要您的 tsconfig.json 中有以下选项,打字稿就会自动检测导入的 JSON 文件的类型:

"resolveJsonModule": true,
"esModuleInterop": true,

之后,您可以获得 JSON 对象的所有键,类型如下:

import jsonFile from '../public/sample.json'

type TypeKeyValue = keyof typeof jsonFile 

如果您还需要所有嵌套路径,则需要执行 :

type Join<K, P> = K extends string | number ?
    P extends string | number ?
    `${K}${"" extends P ? "" : "."}${P}`
    : never : never;


type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
    11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...0[]]

type Paths<T, D extends number = 10> = [D] extends [never] ? never : T extends object ?
    { [K in keyof T]-?: K extends string | number ?
        `${K}` | Join<K, Paths<T[K], Prev[D]>>
        : never
    }[keyof T] : ""

type Leaves<T, D extends number = 10> = [D] extends [never] ? never : T extends object ?
    { [K in keyof T]-?: Join<K, Leaves<T[K], Prev[D]>> }[keyof T] : "";

type Nested = Paths<typeof json>

在这种情况下 Nested 将具有对象的所有路径。

一开始你可以使用

type TypeKeyValue: keyof typeof jsonFile;

这将为您提供根级密钥。