Home > Software engineering >  Type return value of function with generics in TypeScript
Type return value of function with generics in TypeScript

Time:10-16

I was playing around with generics, and I bumped into code that I didn't quite understand, I have a function that receives a resp object; it could be of either type TSuccess or TFailure, they have different structures, and I want to get a return type according to the incoming type. For that, I have used generics. It looks like the code is running, but I still have squiggly lines and the error

"Type '"successful"' is not assignable to type 'T extends TFailure ? "failed" : T extends TSuccess ? "successful" : never'.(2322)"

I ran code on Stackblitz, click to follow to playground

Am I doing something wrong here?

THANK YOU FOR ANSWERING!

  • Related