microsoft / microsoft/TypeScript
Ignored check result of typeof a === typeof b
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.2.0-dev.20181025
Search Terms:
Code
type argType = string | number | boolean;
function sort(a: argType, b: argType): number {
if (typeof a !== typeof b) {
return 0;
}
// typeof a should be number now
if (typeof a !== 'number') {
return 0;
}
// typeof b should be number now
return a - b; // Here TS is not sure that b is number
}
Expected behavior:
We have already checked that typeof a is equal typeof b. So if typeof a is number, than and b is number too.
Actual behavior:
error: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
Related Issues:
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 code sample and Playground link, then reproduce the reported error using the stated TypeScript 3.2.0-dev.20181025 version or a current build. Investigate control-flow narrowing for typeof a !== typeof b before the arithmetic expression. Done means the example correctly recognizes b as a number without weakening related type checking.
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
- Mostly clear
- Newbie friendliness
- 38/100