Skip to content

Contextual parameters inferred from overloads improvements #59350

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

Open
6 tasks done
ssalbdivad opened this issue Jul 18, 2024 · 0 comments
Open
6 tasks done

Contextual parameters inferred from overloads improvements #59350

ssalbdivad opened this issue Jul 18, 2024 · 0 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@ssalbdivad
Copy link

πŸ” Search Terms

contextual parameters functions overloads inference TS7006 argument length

βœ… Viability Checklist

⭐ Suggestion

Currently, inferring a parameter type from a function with overloads is very finicky. Unused generic parameters, constraints, and parameter length all unnecessarily prevent parameter types from being inferred.

A lot of these limitations are mentioned on #42620, but there's no inherent reason the parameters can't be inferred in many of these cases. This is a suggestion to revisit some of the merge logic to allow a wider variety of cases to be inferred correctly.

πŸ“ƒ Motivating Example

Here are some examples I've run into recently that I believe are reasonable inference candidates:

Playground: https://tsplay.dev/N5YkVN

export type Fn = {
    // t is unused in the first overload
    <t>(s: string): void
    <t>(t: t): void
}

// first: string | t
export const fn: Fn = (first) => {}

export type Fn2 = {
    // remove unused generic param
    (s: string): void
    <t>(t: t): void
}

// Parameter 'first' implicitly has an 'any' type.
export const fn2: Fn2 = (first) => {}

export type Fn3 = {
    <t>(s: string): void
    // add constraint to a parameter
    <t extends number>(t: t): void
}

// Parameter 'first' implicitly has an 'any' type.
export const fn3: Fn3 = (first) => {}


export type Fn4 = {
    (s: string): void
    (n: number, b: boolean): void
}

// Second could safely be inferred as boolean | undefined here

// Type '(first: number, second: boolean) => void' is not assignable to type 'Fn4'.
//   Target signature provides too few arguments. Expected 2 or more, but got 1.(2322)
export const fn4: Fn4 = (first, second) => {}

πŸ’» Use Cases

  1. What do you want to use this for? Parameter inference
  2. What shortcomings exist with current approaches? Duplicate definitions and casting
  3. What workarounds are you using in the meantime? Forcing the signatures to align with hacks like adding unused generic parameters, manually typing them and/or casting.
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants