You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a function, that should return ResponseA when a parameter is true, or ResponseB otherwise. The return on the execution works fine, but when returning it inside the function, there's a type error, needing to use as any to fix it
🔎 Search Terms
I've tried to search about conditional returns by functions, but didn't find any regarding this issue
🕗 Version & Regression Information
Tried on 4.7.4 and the nightly version
This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional type
interfaceResponseA{fooBar: string}interfaceResponseB{specialNumber: number}typeShouldGetFirst<Rextends[unknown,unknown],SHOULDextendsboolean>=SHOULDextendstrue ? R[0] : R[1]functiongetInfo<Bextendsboolean>(format: B): ShouldGetFirst<[ResponseA,ResponseB],B>{if(format){return{fooBar: 'hello world'// Type '{ fooBar: string; }' is not assignable to type 'ShouldGetFirst<[ResponseA, ResponseB], B>'}}else{return{specialNumber: 42// Type '{ specialNumber: number; }' is not assignable to type 'ShouldGetFirst<[ResponseA, ResponseB], B>'}}}consta=getInfo(true)// a is ResponseAconstb=getInfo(false)// b is ResponseB
🙁 Actual behavior
The return statements are returning an not assignable error, even that, when executing the function(as seen on the end), the response types are correct.
🙂 Expected behavior
The problem should not exist, so it's not needed to use as any
The text was updated successfully, but these errors were encountered:
Resolving of conditional types involving unbound generic type arguments is deferred. At that point the compiler doesn't know what ShouldGetFirst<[ResponseA, ResponseB], B> resolves to, because the compiler doesn't know what type B is. Constraints are not taken into consideration. Same as #48243.
When actually calling the function the type argument is not unbound anymore, so the conditional type can be resolved.
Bug Report
I'm working on a function, that should return
ResponseA
when a parameter is true, orResponseB
otherwise. The return on the execution works fine, but when returning it inside the function, there's a type error, needing to useas any
to fix it🔎 Search Terms
I've tried to search about conditional returns by functions, but didn't find any regarding this issue
🕗 Version & Regression Information
Tried on 4.7.4 and the nightly version
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The return statements are returning an not assignable error, even that, when executing the function(as seen on the end), the response types are correct.
🙂 Expected behavior
The problem should not exist, so it's not needed to use
as any
The text was updated successfully, but these errors were encountered: