对象属性路径的 TypeScript 类型定义 [英] TypeScript type definition for an object property path

查看:99
本文介绍了对象属性路径的 TypeScript 类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

示例:

const object1 = {someProperty: 真};const object2 = {嵌套对象:object1,另一个属性:2};type PropertyPath= [keyof Type, ...Array];//<-- 这需要改进//----------------------------------------------------------------let propertyPath1: PropertyPath;propertyPath1 = ["someProperty"];//有效propertyPath1 = ["doesntExist"];//不应该工作let propertyPath2: PropertyPath;propertyPath2 = ["nestedObject", "someProperty"];//有效propertyPath2 = ["nestedObject", "doesntExist"];//不应该工作propertyPath2 = ["doesntExist"];//不应该工作

链接到打字原稿操场

解决方案

这个重复的问题的答案你可以使用递归的 Paths<>Leaves<> 类型别名,具体取决于您是否要支持从根开始并在任何位置结束的所有路径树 (Paths<>) 或者如果您只想支持从根开始到树的叶子结束的路径 (Leaves<>):

type AllPathsObject2 = Paths;//输入 AllPathsObject2 = ["nestedObject"] |["nestedObject", "someProperty"] |//[另一个属性"]type LeavesObject2 = Leaves;//type LeavesObject2 = ["nestedObject", "someProperty"] |[另一个属性"]

我假设它是 Paths,但如果适合您的用例,您可以将其更改为 Leaves.这是您得到的行为,它符合您的要求:

let propertyPath1: Paths;propertyPath1 = ["someProperty"];//有效propertyPath1 = ["doesntExist"];//错误!//~~~~~~~~~~~~~~let propertyPath2: Paths;propertyPath2 = ["nestedObject", "someProperty"];//有效propertyPath2 = ["nestedObject", "doesntExist"];//错误!//~~~~~~~~~~~~~propertyPath2 = ["doesntExist"];//错误!//~~~~~~~~~~~~~

好的,希望有帮助;祝你好运!

rel 代码 rSrz+sGvyCohp rl rz+sGvyCowlp>链接代码

Is it possible to type an array of strings in such a way that the array can only be a valid property path in a given object? The type definition should work for all deeply nested objects.

Example:

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

Link to TypeScript playground

解决方案

In the answer to the question this duplicates you can use the recursive Paths<> or Leaves<> type aliases, depending on whether or not you want to support all paths that start at the root and end anywhere in the tree (Paths<>) or if you want to only support paths that start at the root and end at the leaves of the tree (Leaves<>):

type AllPathsObject2 = Paths<typeof object2>;
// type AllPathsObject2 = ["nestedObject"] | ["nestedObject", "someProperty"] | 
//  ["anotherProperty"]

type LeavesObject2 = Leaves<typeof object2>;
// type LeavesObject2 = ["nestedObject", "someProperty"] | ["anotherProperty"]

I'll assume it's Paths but you can change it to Leaves if that fits your use case. Here's the behavior you get, which matches what you asked for:

let propertyPath1: Paths<typeof object1>;
propertyPath1 = ["someProperty"]; // works
propertyPath1 = ["doesntExist"]; // error!
//               ~~~~~~~~~~~~~~

let propertyPath2: Paths<typeof object2>;
propertyPath2 = ["nestedObject", "someProperty"]; // works
propertyPath2 = ["nestedObject", "doesntExist"]; // error!
//                               ~~~~~~~~~~~~~
propertyPath2 = ["doesntExist"]; // error!
//               ~~~~~~~~~~~~~

Okay, hope that helps; good luck!

Link to code

这篇关于对象属性路径的 TypeScript 类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆