GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — instanceof type guard missing deduction · Issue #20138 · microsoft/TypeScript

instanceof type guard missing deduction #20138

New issue
Copy link
New issue
Copy link
Closed
Closed
instanceof type guard missing deduction#20138
Copy link
Assignees
ahejlsberg
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
TypeScript 2.7

Description

@abramobagnara
abramobagnara
opened on Nov 19, 2017
Issue body actions

TypeScript Version: 2.7.0-dev.201xxxxx

Code

class C { }

function f<T extends C>(v: T | string): void {
  if (v instanceof C) {
    const x: T = v;
    console.log(x);
  }
}

Expected behavior:

No compile errors:

Actual behavior:

p.ts(5,11): error TS2322: Type 'string | T' is not assignable to type 'T'.
Type 'string' is not assignable to type 'T'.

Activity

  1. ahejlsberg commented on Nov 19, 2017

    @ahejlsberg
    Anders Hejlsberg (ahejlsberg)
    on Nov 19, 2017
    Member
    More actions

    This was an error in previous releases too because C is an empty object type. However, with #19671 we can (and should) get this right.

  2. Anders Hejlsberg (ahejlsberg)
    self-assigned this
    on Nov 19, 2017
  3. Anders Hejlsberg (ahejlsberg)
    added
    BugA bug in TypeScriptA bug in TypeScript
    on Nov 19, 2017
  4. abramobagnara commented on Nov 19, 2017

    @abramobagnara
    abramobagnara
    on Nov 19, 2017
    Author
    More actions

    From last part of comment above it seems you imply that master branch after #19671 merge get this right, but this is not the case. Am I missing something?

    I can confirm that if C is not an empty object type Typescript 2.6.1 compile the source without errors.

  5. Anders Hejlsberg (ahejlsberg)
    added this to the TypeScript 2.7 milestone on Nov 19, 2017
  6. ahejlsberg commented on Nov 19, 2017

    @ahejlsberg
    Anders Hejlsberg (ahejlsberg)
    on Nov 19, 2017
    Member
    More actions

    abramobagnara I meant that with the approach we introduced in #19671 we can, but currently don't, get it right even when C is an empty class type (as in your example). You are correct that it used to work when C is not an empty class. Both issues are fixed by #20142 that I just put up.

  7. Anders Hejlsberg (ahejlsberg)
    added
    FixedA PR has been merged for this issueA PR has been merged for this issue
    on Nov 19, 2017
  8. Anders Hejlsberg (ahejlsberg)
    closed this as completedin #20142on Nov 20, 2017
  9. microsoft
    locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @ahejlsberg
    Anders Hejlsberg (ahejlsberg)

Labels

BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue

Type

No type

Projects

No projects

    Milestone

    • TypeScript 2.7

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.