microsoft / microsoft/TypeScript
Parameter type narrowing should consider function overload signatures
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.2.1
Code
interface A { x: number; }
interface B { y: number; }
function foo(b: B): void;
function foo(a: A, b: B): void;
function foo(a: A|B, b?: B): void {
if(b !== void 0) {
console.log(a.y); // Error: Property 'y' does not exist on type 'A | B'
}
}
The third (unified) function signature is hidden from the call site, and therefore any arguments should be able to be narrowed based on inspection of the provided arguments. In the case above, the second argument is only defined if the first argument is of type A, and so I would expect narrowing of the argument type to occur accordingly. One could make the argument that in the compiled output, it's possible for JavaScript to still pass subsequent arguments, but then why narrow types at all, seeing as JavaScript can always violate every type constraint that TypeScript makes inferences from?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the TypeScript 2.2.1 reproduction in the issue and inspect how overload signatures relate to the unified implementation signature during parameter narrowing. Verify the current diagnostic for a.y, then determine whether the desired behavior is consistent with the stated overloads. Done means the example narrows the argument as expected without weakening unrelated type checks.
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
- 35/100