对象 属性 路径的 TypeScript 类型定义
TypeScript type definition for an object property path
是否可以键入一个字符串数组,使该数组只能是给定对象中的有效 属性 路径?类型定义应该适用于所有深度嵌套的对象。
示例:
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
可以使用箭头函数
const object1 = {
someProperty: true
};
const object2 = {
nestedObject: object1,
anotherProperty: 2
};
type PropertyPath<Type extends object> = (x: Type) => any;
let propertyPath1: PropertyPath<typeof object1>;
propertyPath1 = (x) => x.someProperty; // works
propertyPath1 = (x) => x.doesntExist; // should not work
let propertyPath2: PropertyPath<typeof object2>;
propertyPath2 = (x) => x.nestedObject.someProperty; // works
propertyPath2 = (x) => x.nestedObject.doesntExist; // should not work
propertyPath2 = (x) => x.doesntExist; // should not work
在 中,您可以使用递归 Paths<>
或 Leaves<>
类型别名,具体取决于您是否要支持从根开始到任何地方结束的所有路径树 (Paths<>
) 或者如果您只想支持从根开始到树叶结束的路径 (Leaves<>
):
type AllPathsObject2 = Paths<typeof object2>;
// type AllPathsObject2 = ["nestedObject"] | ["nestedObject", "someProperty"] |
// ["anotherProperty"]
type LeavesObject2 = Leaves<typeof object2>;
// type LeavesObject2 = ["nestedObject", "someProperty"] | ["anotherProperty"]
我假设它是 Paths
,但如果适合您的用例,您可以将其更改为 Leaves
。这是您得到的行为,它符合您的要求:
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!
// ~~~~~~~~~~~~~
好的,希望对您有所帮助;祝你好运!
是否可以键入一个字符串数组,使该数组只能是给定对象中的有效 属性 路径?类型定义应该适用于所有深度嵌套的对象。
示例:
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
可以使用箭头函数
const object1 = {
someProperty: true
};
const object2 = {
nestedObject: object1,
anotherProperty: 2
};
type PropertyPath<Type extends object> = (x: Type) => any;
let propertyPath1: PropertyPath<typeof object1>;
propertyPath1 = (x) => x.someProperty; // works
propertyPath1 = (x) => x.doesntExist; // should not work
let propertyPath2: PropertyPath<typeof object2>;
propertyPath2 = (x) => x.nestedObject.someProperty; // works
propertyPath2 = (x) => x.nestedObject.doesntExist; // should not work
propertyPath2 = (x) => x.doesntExist; // should not work
在 Paths<>
或 Leaves<>
类型别名,具体取决于您是否要支持从根开始到任何地方结束的所有路径树 (Paths<>
) 或者如果您只想支持从根开始到树叶结束的路径 (Leaves<>
):
type AllPathsObject2 = Paths<typeof object2>;
// type AllPathsObject2 = ["nestedObject"] | ["nestedObject", "someProperty"] |
// ["anotherProperty"]
type LeavesObject2 = Leaves<typeof object2>;
// type LeavesObject2 = ["nestedObject", "someProperty"] | ["anotherProperty"]
我假设它是 Paths
,但如果适合您的用例,您可以将其更改为 Leaves
。这是您得到的行为,它符合您的要求:
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!
// ~~~~~~~~~~~~~
好的,希望对您有所帮助;祝你好运!