Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved type inference for object literals #19513

Merged
merged 18 commits into from
Oct 29, 2017
Merged

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Oct 26, 2017

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.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 31, 2017

files #19613 to track that.

@RafaelSalguero
Copy link

Does this PR also fixes #19843?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants