microsoft / microsoft/TypeScript

Argument types of functions with multiple declarations should be smarter.

Open
#29,369 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Domain: check: Control Flow Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

Suggestion

Argument types of functions with multiple declarations should be smarter.

Use Cases

As showed in example,b should be a number when we are sure that a is "b".But it still think b as a number|string.

Examples

http://www.typescriptlang.org/play/index.html#src=class%20test%7B%0D%0A%20%20%20%20public%20test(a%3A%20%22a%22)%3B%0D%0A%20%20%20%20public%20test(a%3A%20%22b%22%2C%20b%3A%20number)%3B%0D%0A%20%20%20%20public%20test(a%3A%20%22c%22%2C%20b%3A%20string)%3B%0D%0A%20%20%20%20public%20test(a%3A%20%22a%22%20%7C%20%22b%22%20%7C%20%22c%22%2C%20b%3F%3A%20number%20%7C%20string)%20%7B%0D%0A%20%20%20%20%20%20%20%20if%20(a%20%3D%3D%20%22a%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20a%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20b%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20else%20if%20(a%20%3D%3D%20%22b%22)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20a%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20b%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20else%20if%20(a%20%3D%3D%20%22c%22)%20%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20a%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20b%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20else%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20a%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20b%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20public%20testf()%20%7B%0D%0A%20%20%20%20%20%20%20%20this.test(%22a%22)%3B%0D%0A%20%20%20%20%20%20%20%20this.test(%22b%22%2C%201)%3B%0D%0A%20%20%20%20%20%20%20%20this.test(%22c%22%2C%20%221%22)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example from the linked TypeScript Playground and inspect how multiple declarations affect argument types during control-flow narrowing. Done means the implementation and accompanying checks make b reflect the selected declaration when a is narrowed, without changing the shown runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.