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
Please close if this has been asked before - I didn't find anything via search.
Search Terms
Function, overload, multiple signature, discriminate, union
Suggestion
TypeScript should take advantage of refinement to infer which case of an overload we're in.
Use Cases
This would improve the ergonomics of using overloaded function call signatures, and make implementing overloaded functions less tedious.
Examples
typeReservation=voidtypeReserve={(from: Date,to: Date,destination: string): Reservation// 1(from: Date,destination: string): Reservation// 2}letreserve: Reserve=(from: Date,toOrDestination: Date|string,destination?: string)=>{if(toOrDestinationinstanceofDate){destination.toUpperCase()// TSC should infer we're in case 1, so destination must be a string}}
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
It might catch real errors
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
Please close if this has been asked before - I didn't find anything via search.
Search Terms
Function, overload, multiple signature, discriminate, union
Suggestion
TypeScript should take advantage of refinement to infer which case of an overload we're in.
Use Cases
This would improve the ergonomics of using overloaded function call signatures, and make implementing overloaded functions less tedious.
Examples
Checklist
My suggestion meets these guidelines:
Related
The text was updated successfully, but these errors were encountered: