I need to create a constraint that limits keys of TP1
listed by the keyof
keyword to properties that are of a specific type. When I use the builder.setMetadata
method, the first parameter should not accept string "configure" as a valid value (it should accept foo
and other keys that are typed as Foo<something>
). I've tried to come up with a solution by myself, but I'm a bit lost and have been doing it for more than three hours now. Following is the working code:
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
}
}
Solved it by myself using a function: