GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improved type inference for object literals by ahejlsberg · Pull Request #19513 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Oct 26, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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
Anders Hejlsberg (ahejlsberg) added 11 commits October 24, 2017 13:27
@ahejlsberg
Missing properties in fresh object literals are known to be undefined
b17e75b
@ahejlsberg
Normalize object literals in widened union types
b435c90
@ahejlsberg
Normalize nested object literals
1641f9b
@ahejlsberg
Use a single union type for all inferred object literal types
e483ea5
@ahejlsberg
Merge branch 'master' into strictObjectLiterals
0fbe983
@ahejlsberg
Accept API changes
ad3dc20
@ahejlsberg
Accept new baselines
6023433
@ahejlsberg
Ensure void return in forEachType
aa6b438
@ahejlsberg
Add tests
14b7e75
@ahejlsberg
Accept new baselines
2669061
@ahejlsberg
Add missing semicolon
fd0ac15
@ahejlsberg
Anders Hejlsberg (ahejlsberg) requested review from Ryan Cavanaugh (RyanCavanaugh) and Mohamed Hegazy (mhegazy) October 26, 2017 23:01
Anders Hejlsberg (ahejlsberg) added 2 commits October 27, 2017 10:05
@ahejlsberg
Perform subtype reduction when widened union type contains {}
9c27c56
@ahejlsberg
Accept new baselines
39383ca
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Oct 27, 2017
Design Meeting Notes, 10/27/2017 #19536
Closed
Anders Hejlsberg (ahejlsberg) added 5 commits October 27, 2017 17:18
@ahejlsberg
Undo extra subtype reduction in getWidenedType
73802be
@ahejlsberg
Accept new baselines
709541c
@ahejlsberg
Redo subtype reduction with correct --noImplicitAny error reporting
90d20f2
@ahejlsberg
Accept new baselines
4adb15b
@ahejlsberg
Undo unnecessary change
fd0d40c
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit 7798f69 into master Oct 29, 2017
@mhegazy Mohamed Hegazy (mhegazy) mentioned this pull request Oct 31, 2017
DT regression from Improved type inference for object literals #19613
Closed
@mhegazy

Mohamed Hegazy (mhegazy) commented Oct 31, 2017

Copy link
Copy Markdown
Contributor

files #19613 to track that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy
Mohamed Hegazy (mhegazy) deleted the strictObjectLiterals branch October 31, 2017 18:19
@robyoder Rob Yoder (robyoder) mentioned this pull request Dec 31, 2017
Type inference corner case in combination of object spread with destructuring #12532
Closed
@RafaelSalguero

Rafael Salguero Iturrios (RafaelSalguero) commented Jan 19, 2018

Copy link
Copy Markdown

Does this PR also fixes #19843?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@xaviergonz Javier Gonzalez (xaviergonz) mentioned this pull request Feb 18, 2018
fixes for typescript 2.7 mobxjs/mobx-state-tree#667
Merged
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Mar 17, 2018
Bad inference for destructured object parameter with default value #22644
Closed
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) Awaiting requested review from RyanCavanaugh
@mhegazy Mohamed Hegazy (mhegazy) Awaiting requested review from mhegazy

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

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.

3 participants

@ahejlsberg @mhegazy @RafaelSalguero
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.