Giter Site home page Giter Site logo

Comments (3)

fatcerberus avatar fatcerberus commented on May 3, 2024

Duplicate of #42384. Due to various design limitations, type narrowings of object properties currently don't propagate to the containing object except for the special case of discriminated unions.

from typescript.

clouds56 avatar clouds56 commented on May 3, 2024

Yes, it is duplicated, I'm not sure it should be discussed here and #42384 .

Type guards do not propagate type narrowings to parent objects. The narrowing is only applied upon access of the narrowed property which is why the destructing function works, but the reference function does not. Narrowing the parent would involve synthesizing new types which would be expensive. More detail in this comment.

If I read correctly, it is due to performance issue so we choose not to implement this?

I dived into source code and createTypeChecker.checkExpression()
src/compiler/checker.ts#L39525

const type = instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, checkMode);

I think the performance issue is refer to here we won't like to generate a new type every time we checkExpression, even though actually we already have all related information to construct a coerced type here.

Correct me I'm wrong

In current design when checking if a could be called by f1(_), it would

  1. checkExpression(node: Expression | QualifiedName): first calculate type of a, without checking deep "type relation", so it would give Test as is.
  2. checkTypeRelatedTo(source: Type, target: Type): check if Test is compatible with TestArray, without context of a

I see now getTypeOfSourceProperty: (sym: Symbol) => Type used by propertiesRelatedTo is now setting to getNonMissingTypeOfSymbol, is it possible to switch to an Expression awareable callback on error?

from typescript.

typescript-bot avatar typescript-bot commented on May 3, 2024

This issue has been marked as "Duplicate" 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.