简体   繁体   English

打字稿“树”对象定义

[英]Typescript 'tree' object definition

I'm not quite sure of the 'name' for the type of object that I'm creating. 我不太确定要创建的对象类型的“名称”。 I'm calling it a tree because it looks similar to a nested tree without the relationships. 我称它为树是因为它看起来类似于没有关系的嵌套树。 Essentially I want an object with nested definitions like so 本质上,我想要这样的具有嵌套定义的对象

{
    test1: OptionsInterface,
    test2: {
        test3: OptionsInterface,
        test4: {
            test5: OptionsInterface,
        },
    },
}

So the first level can be either OptionsInterface or {[s: string]: OptionsInterface} Is there a way to use this on every 'level' of the object? 因此,第一层可以是OptionsInterface{[s: string]: OptionsInterface}是否可以在对象的每个“层”上使用它?

I've tried defining the above like so: 我尝试过定义上述内容,如下所示:

export default class ApiClient {
    constructor(options: {[s: string]: OptionsInterface | {[s: string]: OptionsInterface}}) {}

But this is only going to be 2 deep right? 但这只会是2深吗? Is there a way I can define my example object without having to manually add every depth? 有没有一种方法可以定义示例对象而无需手动添加每个深度?

use case 用例

I want to be able to call my class like so 我希望能够像这样打电话给我的班级

api = new ApiClient(routeSchema);
await api.call('test2.test4.test5', params);

in call: 通话中:

async call(config: string, variables: object = {}): Promise<Response> {
  const options = get(this.configuration, config);

  if (options === undefined) {
    throw new ConfigNotDefinedExpection(config);
  }

  return await this.callWithOptions(options, variables);
}

Where callWithOptions expects an OptionsInterface 其中callWithOptions需要一个OptionsInterface

Sure, you can do that. 当然可以。

type NestableOptionsInterface = OptionsInterface | { [k: string]: NestableOptionsInterface }

That says a NestableOptionsInterface is either an OptionsInterface or a dictionary whose keys are anything you want and whose values are NestedOptionsInterface . 那表示NestableOptionsInterfaceOptionsInterface或字典,其键可以是您想要的任何键,其值是NestedOptionsInterface So it's a recursive defintion. 因此,这是一个递归定义。 Let's test it: 让我们测试一下:

class Foo {
  constructor(options: NestableOptionsInterface) { }
}

declare const optionsInterface: OptionsInterface;

new Foo(optionsInterface); // okay
new Foo({ a: optionsInterface, b: { c: optionsInterface } }); // okay
new Foo({ a: { b: { c: { d: { e: optionsInterface } } } } }); // okay
new Foo("whoops"); // error
new Foo({ a: optionsInterface, b: { c: "whoops" } }); // error

Looks good. 看起来不错。

If you want to maintain the type of the actual constructor argument, you can use generics like this: 如果要维护实际构造函数参数的类型,可以使用如下泛型:

class Foo<O extends NestableOptionsInterface> {
  constructor(options: O) { }
}

declare const optionsInterface: OptionsInterface;

new Foo(optionsInterface); // Foo<OptionsInterface>
new Foo({ a: optionsInterface, b: { c: optionsInterface } }); // Foo<{ a: OptionsInterface, b:{c: OptionsInterface}}>
new Foo({ a: { b: { c: { d: { e: optionsInterface } } } } }); // Foo<{ a:{b:{c:{d:{e: OptionsInterface}}}}}>

Hope that helps. 希望能有所帮助。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM