Is it feasible to create a custom type in TypeScript that can check if a given key exists within the Type definition? I am specifically using noUncheckedIndexAccess: true
configuration.
interface one {
foo: string;
bar: string;
}
interface two {
bar: string;
foobar: string;
}
interface three {
foobar: string;
foo: string;
}
interface SomeType<T> {
..?..
}
function fn<T>(object: SomeType<T>, key: T) {
object[key] // is defined
//...
}
fn<"foo">(arg); // Typeof arg must be (one | three)
fn<"bar">(arg); // Typeof arg must be (one | two)