当然,你可以。
type NestableOptionsInterface = OptionsInterface | { [k: string]: NestableOptionsInterface }
上面写着
NestableOptionsInterface
不是
OptionsInterface
或者一本字典,它的键是你想要的任何东西,它的值是
NestedOptionsInterface
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
看起来不错。
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}}}}}>
希望能有所帮助。祝你好运!