-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This diffs adds typing support for object type spread properties. = Overview For example, a type of the form `{...A,...B}` is an object with the properties of `A` and `B`. If some property `p` appears in both `A` and `B`, the resulting type will use the property type from `B`. That is, spreads overwrite earlier properties. = Motivation Currently, people use intersections for a similar result, but `A & B` doesn't behave the way people usually expect when the property sets are not disjoint. Furthermore, it's not possible to use intersections for exact object types, even when the property sets are disjoint. That is, no object is the inhabitant of the type `{| p: T |} & {| q: U |}`. But `{| ...A, ...B |}`, where `A` is `{| p: T |}` and `B` is `{| q: U |}` does what you expect. = Relationship with runtime object spread The rules for producing a resulting object type are derived from an idealized view of runtime object spread. That is, the type `{...A,...B}` is intended to be the type of `{...a,...b}` given `a:A` and `b:B`. At runtime, object spread is concerned with the layout of properties. Only the own properties are considered. Flow's object types don't model layout, so a value with type `{p:T}` might have an own property `p` or might get `p` from anywhere in it's prototype chain. Eventually we will change exact object types to imply all-own properties. For example, a value with the type `{|p:T|}` will definitely have an own property `p` with type `T`. The logic used in this diff assumes that exact-implies-own holds. Assuming this makes it possible to implement object type spread without waiting for the exact object work to be completed. Eventually, once exact does in fact imply own, we can use the same logic for object value spread and replace current, buggy spread implementation. = Unions A spread of unions is equal to a union of spreads. `{...A|B}` is equal to `{...A}|{...B}`. Spreading unions and non-unions leads to forking behavior. `{...A|B,...C}` is equal to `{...A,...C}|{...B,...C}`, for example. = Intersections A spread of intersections is equal to a spread of a merged intersection type. `{...{p:T}&{q:U}}` is equal to `{...{p:T,q:U}`. Intersections distribute through unions. `{...A&(B|C)}` is eequal to `{...(A&B)|(A&C)}`, for example. = Instance types Instance types add a small additional complication, because the super class's class properties represent own properties. To satisfy this, we walk up the super classes until we hit a non-instance, collecting own properties as we go. Instances are modeled as inexact objects w.r.t. spread. Reviewed By: gabelevi Differential Revision: D4268886 fbshipit-source-id: c1d6bb8d6d2b6b90c08095e8980a24bc0b6c25ff
- Loading branch information
1 parent
160dde1
commit ad443dc
Showing
23 changed files
with
1,010 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.