interface AddDataRequest{
data:any
}
interface AddDataResponse{
id:string
}
interface ITest{
addData(json:AddDataRequest):Promise<AddDataResponse>
removeData(json:AddDataResponse):Promise<boolean>
}
function testInterface<A extends keyof ITest>(action:A,req:Parameters<ITest[A]>[0],res:Awaited<ReturnType<ITest[A]>>){
if(action === 'addData'){
console.log(res.id); //TS2339: Property 'id' does not exist on type 'boolean | AddDateResponse'. Property 'id' does not exist on type 'false'.
}
else if(action==='removeData'){
console.log(req.id); //TS2339: Property 'id' does not exist in the 'AddDateRequest | AddDateResponse' type. The property 'id' is not found in the 'AddDateRequest' type.
}
}
testInterface('removeData', {id:'test123'},true); //But works here
I am attempting to establish a type dependency of function parameters from the first one. It functions correctly when calling the function but encounters errors within it. These errors are elaborated upon in the comments inside the code.