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

support narrowing types of function arguments based on overloads #46422

Closed
5 tasks done
DetachHead opened this issue Oct 19, 2021 · 3 comments
Closed
5 tasks done

support narrowing types of function arguments based on overloads #46422

DetachHead opened this issue Oct 19, 2021 · 3 comments

Comments

@DetachHead
Copy link
Contributor

Suggestion

πŸ” Search Terms

overload narrowing

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

it would be nice if you could rely on function overloads to more easily narrow parameters

πŸ“ƒ Motivating Example

in this case, it's impossible for value2 to be undefined if value1 isn't

function foo(): void
function foo(value1: number, value2: number): void
function foo(value1?: number, value2?: number): void {
    if (typeof value1 !== 'undefined') {
        //Type 'number | undefined' is not assignable to type 'number'.
        // Type 'undefined' is not assignable to type 'number'.(2322)
        const bar: number = value2
    }
}
@fatcerberus
Copy link

I know there's at least one other issue discussing this, but I don't know what to search for to find it.

@MartinJohns
Copy link
Contributor

Related: #28167

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 19, 2021

Duplicate of #14515.

@fatcerberus: Just search for overloads in:title

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants