I am currently working on creating a generic type that takes a root-level property name and returns a union type of a property nested underneath it. For instance:
interface operations {
updateSomething: {
"201": {
schema: number;
};
"400": {
schema: string;
};
};
}
If I want to retrieve the "schemas" for the type updateSomething
, it should result in number | string
. The non-generic version functions accurately:
type UpdateSomethingSchema =
operations["updateSomething"][keyof operations["updateSomething"]]["schema"];
// string | number ✓
In attempting to write a generic type, I encounter this error:
Type '"schema"' cannot be used to index type 'operations[O][keyof operations[O]]'.ts(2536)
Curiously, despite the error message, the type appears to work as intended when implemented:
type UpdateSomethingSchema = SchemaOf<"updateSomething">;
// string | number ✓
Is there an error in my approach, or is this simply a limitation of TypeScript?