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

Strange stateful behavior with inferred type predicate type #17451

Closed
ghost opened this issue Jul 27, 2017 · 0 comments · Fixed by #17600
Closed

Strange stateful behavior with inferred type predicate type #17451

ghost opened this issue Jul 27, 2017 · 0 comments · Fixed by #17600
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@ghost
Copy link

ghost commented Jul 27, 2017

TypeScript Version: nightly (2.5.0-dev.20170725)

Code

function tryCast<T, U extends T>(value: T, predicate: (t: T) => t is U): U | undefined {
    return predicate(value) ? value : undefined;
}

declare const sn: string | number;

const x: number | undefined = tryCast(sn, (n): n is number => typeof n === "number"); // Always works

const y = tryCast(sn, (n): n is number => typeof n === "number");

const z: number | undefined = y;

Expected behavior:

No error.

Actual behavior:

src/a.ts(11,7): error TS2322: Type 'string | number | undefined' is not assignable to type 'number | undefined'.
  Type 'string' is not assignable to type 'number | undefined'.

Depending on the order I hover over things, hovering over tryCast and y might show a type of string | number | undefined, but it might show a type of number | undefined. (But the error at z is always present.)

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jul 27, 2017
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.5 milestone Jul 27, 2017
@ahejlsberg ahejlsberg assigned ghost and unassigned ahejlsberg Jul 28, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.6, TypeScript 2.7 Oct 9, 2017
@ghost ghost closed this as completed in #17600 Dec 5, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 11, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants