Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smarter case selection for overloaded functions #25497

Closed
4 tasks done
bcherny opened this issue Jul 7, 2018 · 2 comments
Closed
4 tasks done

Smarter case selection for overloaded functions #25497

bcherny opened this issue Jul 7, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@bcherny
Copy link

bcherny commented Jul 7, 2018

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

type Reservation = void

type Reserve = {
  (from: Date, to: Date, destination: string): Reservation // 1
  (from: Date, destination: string): Reservation // 2
}

let reserve: Reserve = (from: Date, toOrDestination: Date | string, destination?: string) => {
  if (toOrDestination instanceof Date) {
     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)

Related

@RyanCavanaugh
Copy link
Member

Looks like #14515 ?

@bcherny
Copy link
Author

bcherny commented Jul 9, 2018

Dup of #14515.

@bcherny bcherny closed this as completed Jul 9, 2018
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants