【发布时间】:2017-05-30 17:18:22
【问题描述】:
我需要创建一个约束,将keyof 关键字列出的TP1 的键限制为特定类型的属性。当我使用builder.setMetadata 方法时,第一个参数不应该接受字符串“configure”作为有效值(它应该接受foo 和其他键入为Foo<something> 的键)。我试图自己想出一个解决方案,但我有点迷茫,现在已经做了三个多小时。以下是工作代码:
interface Foo<T> {};
class Test { prop: string; }
class Builder<T> {
public setMetadata<TP1 extends keyof this, TP2 extends keyof this[TP1]>(prop: TP1, propOfProp: TP2) {
// ...
}
}
class Bar {
foo: Foo<Test>;
configure(builder: Builder<Bar>) {
builder.setMetadata("", ""); // only "foo" should be accepted value in the first argument, "configure" shouldn't be in the list
}
}
【问题讨论】:
标签: typescript types algebraic-data-types complex-data-types