microsoft / microsoft/TypeScript
Assignment of argument to argument loses deduced typing
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Could be a duplicate but difficult to construct an issue search to discern.
Problem shows with typescript@latest version 3.0.1.
Given nature of problem suspect that it would not have been fixed in typescript@next. Apologies for not testing.
type inference error argument
Code
// A *self-contained* demonstration of the problem follows...
// Just paste this code into your favorite TypeScript IDE
function foobar_good(arg: unknown): void | number {
if (typeof arg != 'number') {
return
}
return arg;
}
function foobar_bad(arg: unknown): void | number {
if (typeof arg != 'number') {
return
}
arg = arg;
return arg;
}
[ts] Type 'unknown' is not assignable to type 'number | void'.
Type 'unknown' is not assignable to type 'void'.
Expected behavior:
Should compile.
Actual behavior:
Does not compile.
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 self-contained TypeScript example in the issue and compile it with TypeScript 3.0.1, comparing foobar_good with foobar_bad. Investigate how assigning arg to itself affects narrowing from unknown to number. Done means the shown code compiles while preserving the expected type-checking behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100