Comments (5)
This is expected subtype reduction behavior. Typescript can only infer your desired type in specific scenarios with fresh objects; see #19513.
from typescript.
@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.
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.
@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:
{p: number} | {p:number, m: string}
-m
cannot be used without type narrowing- or
{p: number, m: unknown} | {p:number, m: string}
,m
isunknown
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.
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)
- [isolatedDeclarations] Optional parameter in arrow function typed as imported generic type gets emitted as `any`
- Easier use of indexed access types on nullable/optional types HOT 2
- Narrow conditional type HOT 1
- Error for path not found when ended with asterisk
- `tryGetPerformanceHooks` throws on Stackblitz with ts 5.5.3 HOT 3
- Avoid providing promise methods as completions at return positions in non-async functions
- Consider disallowing `in` operator use with arbitrary key operands on closed types for which the keys are known HOT 2
- Convert to ESM uses template strings instead of string literals HOT 1
- Running type checker produces js out of memory error HOT 2
- Callback argument type not inferred for union of interfaces HOT 1
- Como Hackear Instagram 2024 Hackear Instagram Metodos Atualizados #221857
- [transpileDeclaration API] ts.transpileDeclaration always returns empty sourceMapText
- Incorrect Error Message: "Cannot assign to 'name' because it is a constant" Instead of "Identifier 'name' is not defined" for Undefined Identifier Assignment HOT 2
- Generic with extends yields to different type than directly specifying it HOT 4
- Instantiation expressions don't reject incompatible signatures in situations with mixed call and construct signatures HOT 1
- Parameters of overridden methods are made implicit any when using JSDoc `@override` HOT 13
- Auto complete and auto import not work (ReactJS, ChakraUI)
- 'TypeScript and JavaScript Language Features' Formatter hangs HOT 3
- Mapped type of a conditional type unexpectedly fails inference
- Returing a generic argument from a function allows returning a wrong type HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.