Trying to get type, but sth gone wrong:
public async request<KModel extends keyof RequestTypes, KMethod extends keyof RequestTypes[KModel]>(modelName: KModel, calledMethod: KMethod, methodProperties: RequestTypes[KModel][KMethod]["request"]): Promise<RequestTypes[KModel][KMethod]["response"]> {};
interface RequestTypes {
InternetDocument: {
save: {
request: a965630e_8512_11ec_8ced_005056b2dbe1_Request,
response: a965630e_8512_11ec_8ced_005056b2dbe1_Response
}
}
}
TS error:
Type '"request"' cannot be used to index type 'RequestTypes[KModel][KMethod]'.ts(2536)
CodePudding user response:
Not sure why this exactly fixes issue or is there a better way, I think just that typescript cannot be sure that for each Request type there will be reqest and response ?? altough it doesnt make sense, anyways, using conditionals helped.
public async request<
KModel extends keyof RequestTypes,
KMethod extends keyof RequestTypes[KModel]
>(
modelName: KModel,
calledMethod: KMethod,
methodProperties: "request" extends keyof RequestTypes[KModel][KMethod]
? RequestTypes[KModel][KMethod]["request"]
: never
): Promise<
"response" extends keyof RequestTypes[KModel][KMethod]
? RequestTypes[KModel][KMethod]["response"]
: never
> {}