相关疑难解决方法(0)

Typescript:嵌套对象的深层 keyof,具有相关类型

我正在寻找一种拥有嵌套对象的所有键/值对的方法。

(用于 MongoDB 点符号键/值类型的自动完成)

interface IPerson {
    name: string;
    age: number;
    contact: {
        address: string;
        visitDate: Date;
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我想要实现的目标,使其成为:

type TPerson = {
    name: string;
    age: number;
    contact: { address: string; visitDate: Date; }
    "contact.address": string;
    "contact.visitDate": Date;
}
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

在这个答案中,我可以得到密钥Leaves<IPerson>。于是就变成了'name' | 'age' | 'contact.address' | 'contact.visitDate'

在 @jcalz 的另一个答案中,我可以通过DeepIndex<IPerson, ...>.

是否有可能将它们组合在一起,成为类似的类型TPerson

修改 9/14:用例,需要和不需要:

当我开始这个问题时,我在想它可以像这样简单[K in keyof T]: T[K];,只需进行一些巧妙的转换。但是我错了。这是我需要的:

1. 索引签名

所以界面

interface IPerson { …
Run Code Online (Sandbox Code Playgroud)

javascript mongodb mongodb-query typescript typescript-generics

15
推荐指数
2
解决办法
8990
查看次数

为什么我收到“类型实例化过深并且可能是无限的”?

游乐场链接

我有这些一般定义:

type Module<P extends Payloads, C extends Children> = {
    payloads: P;
    children: C;
};

type Children = Record<string, any>; // some values will be nested Modules

type Payloads = Record<string, any>;

type ExtractPayloads<M extends Module<any, any>> = M extends Module<infer P, any> ? P : never;

type ExtractChildren<M extends Module<any, any>> = M extends Module<any, infer C> ? C : never;
Run Code Online (Sandbox Code Playgroud)

基本上,模块是指定类型的类型children,可以包含嵌套模块。

我有这种类型,可以Action根据模块的payload类型生成 s:

type ModuleRootActions<
  MODULE extends Module<any, any>,
  PAYLOADS = …
Run Code Online (Sandbox Code Playgroud)

typescript typescript-generics

14
推荐指数
3
解决办法
3万
查看次数

对象属性路径的 TypeScript 类型定义

是否可以以这样的方式键入字符串数组,使得该数组只能是给定对象中的有效属性路径?类型定义应该适用于所有深度嵌套的对象。

例子:

const object1 = {
    someProperty: true
};
const object2 = {
    nestedObject: object1,
    anotherProperty: 2
};

type PropertyPath<Type extends object> = [keyof Type, ...Array<string>]; // <-- this needs to be improved

// ----------------------------------------------------------------

let propertyPath1: PropertyPath<typeof object1>;

propertyPath1 = ["someProperty"]; // works
propertyPath1 = ["doesntExist"]; // should not work

let propertyPath2: PropertyPath<typeof object2>;

propertyPath2 = ["nestedObject", "someProperty"]; // works
propertyPath2 = ["nestedObject", "doesntExist"]; // should not work
propertyPath2 = ["doesntExist"]; // should not work
Run Code Online (Sandbox Code Playgroud)

链接到 TypeScript 游乐场

reflection recursion types typescript jsonpointer

11
推荐指数
1
解决办法
2万
查看次数

如何使用 TypeScript 键入检查 i18n 词典?

是否可以在react-i18next字典中键入检查现有键?因此,如果密钥不存在,TS 会在编译时警告您。

例子。

假设,我们有这本字典:

{
  "footer": {
    "copyright": "Some copyrights"
  },

  "header": {
    "logo": "Logo",
    "link": "Link",
  },
}
Run Code Online (Sandbox Code Playgroud)

如果我提供不存在的密钥,TS 应该会爆炸:

const { t } = useTranslation();

<span> { t('footer.copyright') } </span> // this is OK, because footer.copyright exists
<span> { t('footer.logo') } </span> // TS BOOM!! there is no footer.logo in dictionary
Run Code Online (Sandbox Code Playgroud)

这种技术的正确名称是什么?我很确定我不是唯一一个要求这种行为的人。

它是react-i18next开箱即用的吗?是否有 APIreact-i18next以某种方式扩展库以启用它?我想避免创建包装函数。

typescript i18next react-i18next

10
推荐指数
2
解决办法
1万
查看次数

如何使用联合类型深度扁平化 Typescript 接口并在键中保留完整的对象路径

假设我有以下界面:

interface Foo {
  foo: string
  bar?: number
  nested: {
    foo: string,
    deeplyNested: {
      bar?: number
    }
  }
  union: string | {
    foo: string,
    bar?: number
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我需要一种类型来将此接口转换为扁平版本,键中包含完整路径名,如下所示:

interface FooFlattened {
  foo: string
  bar?: number
  "nested.foo": string
  "nested.deeplyNested.bar"?: number

  // if union is a string:
  union: string

  // if union is an object:
  "union.foo": string
  "union.bar"?: number
}
Run Code Online (Sandbox Code Playgroud)

该类型应该适用于任何级别的嵌套和任何数量的联合成员。

我已经在这里找到了一个相关的问题,但这只是一个非常基本的界面,没有联合或可选参数。

typescript

6
推荐指数
1
解决办法
2092
查看次数

在 TypeScript 中展平嵌套对象,同时保留类型

我有一个嵌套对象,我想将其展平,同时还连接嵌套键。我想从这里开始:

const nested = {
  a: {
    b: {
      c: {
        d1: "hello",
        d2: 42,
      },
    },
  },
};
Run Code Online (Sandbox Code Playgroud)

对此:

const flattened = {
  "a.b.c.d1": "hello",
  "a.b.c.d2": 42,
};
Run Code Online (Sandbox Code Playgroud)

如果我有一个执行此转换的函数,例如

function flatten<TNested>(nested: TNested): Flattened<TNested> {...}
Run Code Online (Sandbox Code Playgroud)

是否可以定义Flattened执行正确操作并正确推断 的类型的类型flattened

我知道当前的 TypeScript 可能无法实现这一点,但我想确定一下!

typescript

6
推荐指数
0
解决办法
2683
查看次数

嵌套 keyof 对象路径(使用点表示法)

我正在寻找嵌套的对象路径:这可能吗?

interface IHuman {
    age: number;
    friend: {
        name: string;
    }
}

keyof IHuman; // "age", "friend.name"
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
1
解决办法
805
查看次数

嵌套对象的打字稿字符串点表示法

我有一个嵌套的翻译字符串对象,如下所示:

viewName: {
    componentName: {
        title: 'translated title'
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用一个接受点表示法字符串的翻译库来获取字符串,就像这样translate('viewName.componentName.title')

有什么方法可以强制 translate 的输入参数遵循带有打字稿的对象的形状吗?

我可以通过这样做为第一级做到这一点:

translate(id: keyof typeof languageObject) {
    return translate(id)
}
Run Code Online (Sandbox Code Playgroud)

但我希望这种类型是嵌套的,这样我就可以像上面的例子一样确定我的翻译范围。

typescript

5
推荐指数
2
解决办法
3189
查看次数

如何使用“路径元组”从嵌套属性中检索类型

鉴于 Typescript 中发现的这段(令人惊奇的)代码 :嵌套对象的深层 keyof

type Cons<H, T> = T extends readonly any[] ?
    ((h: H, ...t: T) => void) extends ((...r: infer R) => void) ? R : 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 …
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
1
解决办法
1105
查看次数

如何理解打字稿中类型定义中的 `[0, 1, 2, ...0[]]` ?

我在Typescript: deep keyof of a Nested object中看到了类似这样的类型定义:

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[]]
Run Code Online (Sandbox Code Playgroud)

我不明白最后一部分是什么...0[]意思。

有这方面的文件吗?谢谢

typescript

3
推荐指数
1
解决办法
427
查看次数

具有模板文字类型的属性路径

通过添加模板文字类型,现在可以以类型安全的方式表达属性路径(点符号)。一些用户已经使用模板文字类型实现了一些东西或者提到了它

我想更进一步,并表达类型中空值/未定义/可选的可能性,例如foo.bar?.foobarfoo.boo.far?.farboo编译器应该可以接受,foo.bar.foobar而不适用于以下类型:

type Test = {
  foo: {
    bar?: {
      foobar: never;
      barfoo: string;
    };
    foo: symbol;
    boo: {
      far:
        | {
            farboo: number;
          }
        | undefined;
    };
  };
};
Run Code Online (Sandbox Code Playgroud)

到目前为止,可选参数被拾取(我不知道为什么,但它在我的 IDE 中使用相同的打字稿版本,请参见下面的屏幕截图)但不是明确标记的“远”属性作为未定义。这个游乐场展示了我的进步。不知何故,“未定义检查”没有按预期工作。

IDE 正确展开

typescript

3
推荐指数
1
解决办法
202
查看次数

Typescript:设置深度嵌套对象路径的值类型

我想找到一种方法来拥有value types嵌套对象路径的所有键。

我在一定程度上取得了成功,但未能为数组对象内的深层嵌套属性设置值类型。

interface BoatDetails {
  boats: {
    boat1: string;
    boat2: number;
  };
  ships: Array<Ship>
}
interface Ship {
    shipName: string
}
const boatDetails: BoatDetails = {
  boats: {
    boat1: "lady blue",
    boat2: 1,
  },
  ships: [
      {shipName: "lacrose"}
  ]
};
Run Code Online (Sandbox Code Playgroud)

对于上面的代码,我能够成功设置嵌套对象路径的值类型,例如boats.boat1who value type is stringboats.boat2who value type is numbershipswho value type is Array<Ship>

但无法设置value type嵌套路径ships.0.shipName

我从以下链接中参考了设置深层嵌套对象路径类型: Typescript:嵌套对象的深层 keyof

下面是我在打字稿游乐场中设置深层嵌套对象路径的值类型的尝试:

用于查看深层嵌套对象路径的值类型的 Playground 链接

typescript

1
推荐指数
1
解决办法
2588
查看次数