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 — Constrained types is not assignable to constrainer union types, and failed to narrow its type · Issue #8563 · microsoft/TypeScript

Constrained types is not assignable to constrainer union types, and failed to narrow its type #8563

New issue
Copy link
New issue
Copy link
Closed
Closed
Constrained types is not assignable to constrainer union types, and failed to narrow its type#8563
Copy link
Labels
SuggestionAn idea for TypeScriptAn idea for TypeScript

Description

@falsandtru
falsandtru (falsandtru)
opened on May 11, 2016
Issue body actions

TypeScript Version:

master

Code

function f<T extends string[] | {}>(a: T) {
  if (a instanceof Array) {
    a = a;
  }
}

Expected behavior:

$ node built/local/tsc.js index.ts

Actual behavior:

$ node built/local/tsc.js index.ts
index.ts(3,5): error TS2322: Type 'any[]' is not assignable to type 'T'.

Activity

  1. ahejlsberg commented on May 11, 2016

    @ahejlsberg
    Anders Hejlsberg (ahejlsberg)
    on May 11, 2016
    Member
    More actions

    Hmm. This one is tricky. Currently the only type we consider assignable to a type parameter is that same type parameter (and any of course). In order for the above to work we would basically need to create a new type parameter that is a subtype of T and has a narrowed constraint string[]. It's possible, but it isn't a simple fix.

  2. Anders Hejlsberg (ahejlsberg)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    on May 11, 2016
  3. DanielRosenwasser commented on May 13, 2016

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on May 13, 2016
    Member
    More actions

    I would think that #7422 would have allowed this. Care to weigh in Nathan Shively-Sanders (@sandersn)?

  4. sandersn commented on May 13, 2016

    @sandersn
    Nathan Shively-Sanders (sandersn)
    on May 13, 2016
    Member
    More actions

    Yeah, it did basically what Anders Hejlsberg (@ahejlsberg) said -- but (1) only locally, when getting the narrowed type, and (2) not updated for control-flow rewrite of narrowing. So it can't be used as-is.

  5. ahejlsberg commented on May 13, 2016

    @ahejlsberg
    Anders Hejlsberg (ahejlsberg)
    on May 13, 2016
    Member
    More actions

    I don't think the solution in #7422 works. The thing that makes this issue complicated is that you can't just clone type parameters without also being prepared to treat the clones as aliases for the original type parameter in the type instantiation logic. Otherwise you'll "leak" type parameters.

  6. ahejlsberg commented on May 3, 2017

    @ahejlsberg
    Anders Hejlsberg (ahejlsberg)
    on May 3, 2017
    Member
    More actions

    This has worked correctly for a while now. The type guard produces type T & any[] which is assignable to T so there is no error. I'm closing this issue.

    Reacted by falsandtru
  7. Anders Hejlsberg (ahejlsberg)
    closed this as completedon May 3, 2017
  8. microsoft
    locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      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.