GeezerVison is showing github.com — Improved type inference for object literals by ahejlsberg · Pull Request #19513 · microsoft/TypeScript
Conversation
This PR improves type inference for multiple object literals occurring in the same context. When multiple object literal types contribute to a union type, we now normalize the object literal types such that all properties are present in each constituent of the union type. For example:
const obj = test ? { text: "hello" } : {}; // { text: string } | { text?: undefined }
const s = obj.text; // string | undefined
Previously we inferred type {} for obj and the second line subsequently caused an error because obj would appear to have no properties. That obviously wasn't ideal.
The key insight behind this PR is that for object literal types we know that unspecified properties always have the value undefined. This is unlike regular object types where unspecified properties might have any value. Based on this fact we implement the following new rules:
- When checking a type relationship between a source type S and a target type T, if T is an object literal type, any excess properties in S are required to have type
undefined (normally such excess properties are permitted to have any type).
- When widening a union type, we normalize all constituent types originating in object literals by adding optional properties of type
undefined to each constituent as appropriate such that all constituents have the same set of property names.
- When inferring multiple object literal types for a type parameter, the object literal types are normalized into a single inferred union type.
An example of object literal normalization:
// let obj: { a: number, b: number } |
// { a: string, b?: undefined } |
// { a?: undefined, b?: undefined }
let obj = [{ a: 1, b: 2 }, { a: "abc" }, {}][0];
obj.a; // string | number | undefined
obj.b; // number | undefined
Nested object literals are likewise normalized:
// let obj: { kind: string, pos: { x: number, y: number, a?: undefined, b?: undefined } } |
// { kind: string, pos: { a: string, x?: undefined, y?: undefined, b?: undefined } | { b: number, x?: undefined, y?: undefined, a?: undefined } }
let obj = [{ kind: 'a', pos: { x: 0, y: 0 } }, { kind: 'b', pos: test ? { a: "x" } : { b: 0 } }][0];
obj.kind; // string
obj.pos; // { x: number, y: number, a?: undefined, b?: undefined } | { a: string, x?: undefined, y?: undefined, b?: undefined } | { b: number, x?: undefined, y?: undefined, a?: undefined }
obj.pos.x; // number | undefined
obj.pos.y; // number | undefined
obj.pos.a; // string | undefined
obj.pos.b; // number | undefined
Multiple object literal type inferences for the same type parameter are collapsed into a single normalized union type:
declare function f<T>(...items: T[]): T;
// let obj: { a: number, b: number } |
// { a: string, b?: undefined } |
// { a?: undefined, b?: undefined }
let obj = f({ a: 1, b: 2 }, { a: "abc" }, {});
obj.a; // string | number | undefined
obj.b; // number | undefined
Fixes #19236.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
j-oliveras, danvk, NoelAbrahams, chrisgervang, xorets, jeremistadler, huy-nguyen, isao, Bnaya, CurlerRoo, and 25 more reacted with thumbs up emoji
sean-vieira, j-oliveras, bradleyayers, aciccarello, olaf89, chrisgervang, jeremistadler, Swatinem, isao, zigomir, and 10 more reacted with hooray emoji
fbartho reacted with heart emoji
All reactions
-
👍
35 reactions
-
🎉
20 reactions
-
❤️
1 reaction
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Does this PR also fixes #19843?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|