Giter Club home page Giter Club logo

Comments (12)

jcalz avatar jcalz commented on August 21, 2024 2

Why are we calling this β€œ5.2”?

from typescript.

IllusionMH avatar IllusionMH commented on August 21, 2024 1

Related #16035

from typescript.

ahejlsberg avatar ahejlsberg commented on August 21, 2024 1

Seems to me the real issue is this:

class GenericClass {
    constructor(public value: string) { }
}

class ConcreteClass extends GenericClass {
    constructor(value: string) {
        super(`Concrete: ${value}`);
    }
}

function foo(obj: GenericClass) {
    if (obj.constructor === ConcreteClass) {
        obj;  // never, Wat?
    }
}

It's obviously possible for obj.constructor to be ConcreteClass since that class is derived from GenericClass. The error that this issue reports is simply a follow-on effect of the incorrect narrowing that gets picked up in an inferred type predicate.

from typescript.

fatcerberus avatar fatcerberus commented on August 21, 2024

Changed in 5.2 5.5 so probably a result of automatic type predicate inference

from typescript.

whzx5byb avatar whzx5byb commented on August 21, 2024

The return type of filter callback (element) => element.constructor === ConcreteClass is inferred as element is never in 5.5.2 because of #57465. I would suggest using (element) => element instanceof ConcreteClass instead.

from typescript.

fatcerberus avatar fatcerberus commented on August 21, 2024

Why are we calling this β€œ5.2”?

Oops, typo and/or me being in too much of a rush. Fixed now.

from typescript.

iknowcss-invenco avatar iknowcss-invenco commented on August 21, 2024

I would suggest using (element) => element instanceof ConcreteClass instead.

Yes, I agree this works and is more idiomatic. I will change this in my code to fix the problem. I wanted to report it in case other projects unexpectedly break while trying to do a minor bump. I'm not sure if 5.5.2 fixes a long standing bug or not, but regardless it was a surprise to have a broken build after this minor upgrade. Thanks for your work on it πŸ™

from typescript.

fatcerberus avatar fatcerberus commented on August 21, 2024

@whzx5byb Thats weird though because #16035 suggests this condition doesn’t narrow

from typescript.

whzx5byb avatar whzx5byb commented on August 21, 2024

@fatcerberus Actually the .constructor narrowing works only when the target is a union type and it must be compared using === (or ==, but not for switch clause) operator. I'm very surprised that it even works for the switch (true) pattern as long as there is a === comparison!

class A {
  a!: number;
}

class B {
  b!: number;
}


function fn(input: A | B) {
  switch (input.constructor) {
    case A:
      input.a // <- not work
  }

  switch (true) {
    case input.constructor === A:
      input.a; // work!?
  }
}

But anyway, in the OP's case the narrowing target is not a union type, and .constructor === comparison will always narrow it to never.

from typescript.

ahejlsberg avatar ahejlsberg commented on August 21, 2024

It goes all the way back to #32774 that implemented "constructor type guards". There is no logic to deal with derived classes.

from typescript.

fatcerberus avatar fatcerberus commented on August 21, 2024

#32774 that implemented "constructor type guards"

Wait, why is #16035 still open then?

from typescript.

typescript-bot avatar typescript-bot commented on August 21, 2024

This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.