GithubHelp home page GithubHelp logo

Comments (5)

nmain avatar nmain commented on July 20, 2024 2

This is expected subtype reduction behavior. Typescript can only infer your desired type in specific scenarios with fresh objects; see #19513.

from typescript.

xixixao avatar xixixao commented on July 20, 2024

@nmain can you elaborate? TS correctly infers the type in each branch, and correctly infers the return type when a ternary is used.

Edit: If anything I could imagine the reduction would reduce the return type to {p: number, m?: string} but that's never the case.

from typescript.

nmain avatar nmain commented on July 20, 2024

when a ternary is used

Because they're fresh object literals in that case. Search "subtype reduction" and you'll find various previous issues about this; for instance, #52893.

from typescript.

xixixao avatar xixixao commented on July 20, 2024

@RyanCavanaugh why not infer unknown if undefined cannot be inferred? Why not keep the union? From reading the original PR that's still not clear to me. For my original example, return type of fun could be either be:

  1. {p: number} | {p:number, m: string} - m cannot be used without type narrowing
  2. or {p: number, m: unknown} | {p:number, m: string}, m is unknown without type narrowing.

I would also add to @RyanCavanaugh's comment:

When the complaint is that the behavior is "inconsistent", it seems counterproductive to introduce a new set of rules that make predicting the output much harder than before.

Immediately-returned object? Yes. Anything else? No

Immediately-returned object? Yes. Intermediate const? Maybe, depends on the initializer. Function calls? Method calls? Passing the object somewhere else? What if the object goes into a generic call? Epicycles.

There will be confusion somewhere. But "you have a function call here, passing in the object, so we can't tell whether there are extra properties on it afterwards" is less confusing than the current behavior imo (this was a common issue in Flow, but people working with it learned it). So tracking object literals / "closeness" would be preferable.

from typescript.

typescript-bot avatar typescript-bot commented on July 20, 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.