microsoft / microsoft/TypeScript

Assignment of argument to argument loses deduced typing

Open
#26,397 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Control Flow
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.