Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment of argument to argument loses deduced typing #26397

Open
ghost opened this issue Aug 12, 2018 · 1 comment
Open

Assignment of argument to argument loses deduced typing #26397

ghost opened this issue Aug 12, 2018 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@ghost
Copy link

ghost commented Aug 12, 2018

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.

@ghost
Copy link

ghost commented Aug 12, 2018

Related to #25720. We should handle assignments to unknown as narrowing. Simplified repro:

function f(p: unknown): number {
	p = 2;
	return p * 2;
}

@ghost ghost added the Bug A bug in TypeScript label Aug 12, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.3 milestone Oct 9, 2018
@sandersn sandersn modified the milestones: TypeScript 3.4.0, Backlog Mar 13, 2019
@sandersn sandersn removed their assignment Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants