Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13.7k
Intersection types#3622
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
Uh oh!
There was an error while loading. Please reload this page.
Intersection types #3622
Changes from all commits
643832dd2955e8d571034529fcfdc623e6c185881090ffdf7a13b642c517a636ae775bfb1bf4230d15a8144a635fc1888eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -191,6 +191,7 @@ namespace ts { | ||
| ArrayType, | ||
| TupleType, | ||
| UnionType, | ||
| IntersectionType, | ||
| ParenthesizedType, | ||
| // Binding patterns | ||
| ObjectBindingPattern, | ||
| @@ -667,10 +668,14 @@ namespace ts { | ||
| elementTypes: NodeArray<TypeNode>; | ||
| } | ||
| export interface UnionTypeNode extends TypeNode { | ||
| export interface UnionOrIntersectionTypeNode extends TypeNode { | ||
| types: NodeArray<TypeNode>; | ||
| } | ||
| export interface UnionTypeNode extends UnionOrIntersectionTypeNode { } | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add brands so that these UnionTypeNode and IntersectionTypeNode are distinguished and not assignable to each other. | ||
| export interface IntersectionTypeNode extends UnionOrIntersectionTypeNode { } | ||
| export interface ParenthesizedTypeNode extends TypeNode { | ||
| type: TypeNode; | ||
| } | ||
| @@ -1573,7 +1578,7 @@ namespace ts { | ||
| Merged = 0x02000000, // Merged symbol (created during program binding) | ||
| Transient = 0x04000000, // Transient symbol (created during type check) | ||
| Prototype = 0x08000000, // Prototype property (no source representation) | ||
| UnionProperty= 0x10000000, // Property in union type | ||
| SyntheticProperty = 0x10000000, // Property in union or intersection type | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we use "UnionOrIntersection" elsewhere. It seems like another place we'd want to use them. i.e. "UnionOrIntersectionProperty". "Synthetic" is odd, as it's a term we don't use consistently for this concept elsewhere. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with Cyrus Najmabadi (@CyrusNajmabadi). After reading the code, it will be much easier to read if it is name Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, also instantiated properties are just as synthetic as union/intersection properties. | ||
| Optional = 0x20000000, // Optional property | ||
| ExportStar = 0x40000000, // Export * declaration | ||
| @@ -1652,7 +1657,7 @@ namespace ts { | ||
| instantiations?: Map<Type>; // Instantiations of generic type alias (undefined if non-generic) | ||
| mapper?: TypeMapper; // Type mapper for instantiation alias | ||
| referenced?: boolean; // True if alias symbol has been referenced as a value | ||
| unionType?: UnionType; // Containing union type for union property | ||
| containingType?: UnionOrIntersectionType; // Containing union or intersection type for synthetic property | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. align comments. | ||
| resolvedExports?: SymbolTable; // Resolved exports of module | ||
| exportsChecked?: boolean; // True if exports of external module have been checked | ||
| isNestedRedeclaration?: boolean; // True if symbol is block scoped redeclaration | ||
| @@ -1721,17 +1726,18 @@ namespace ts { | ||
| Interface = 0x00000800, // Interface | ||
| Reference = 0x00001000, // Generic type reference | ||
| Tuple = 0x00002000, // Tuple | ||
| Union = 0x00004000, // Union | ||
| Anonymous = 0x00008000, // Anonymous | ||
| Instantiated = 0x00010000, // Instantiated anonymous type | ||
| Union = 0x00004000, // Union (T | U) | ||
| Intersection = 0x00008000, // Intersection (T & U) | ||
| Anonymous = 0x00010000, // Anonymous | ||
| Instantiated = 0x00020000, // Instantiated anonymous type | ||
| /* @internal */ | ||
| FromSignature = 0x00020000, // Created for signature assignment check | ||
| ObjectLiteral = 0x00040000, // Originates in an object literal | ||
| FromSignature = 0x00040000, // Created for signature assignment check | ||
| ObjectLiteral = 0x00080000, // Originates in an object literal | ||
| /* @internal */ | ||
| ContainsUndefinedOrNull = 0x00080000, // Type is or contains Undefined or Null type | ||
| ContainsUndefinedOrNull = 0x00100000, // Type is or contains Undefined or Null type | ||
| /* @internal */ | ||
| ContainsObjectLiteral = 0x00100000, // Type is or contains object literal type | ||
| ESSymbol = 0x00200000, // Type of symbol primitive introduced in ES6 | ||
| ContainsObjectLiteral = 0x00200000, // Type is or contains object literal type | ||
| ESSymbol = 0x00400000, // Type of symbol primitive introduced in ES6 | ||
| /* @internal */ | ||
| Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null, | ||
| @@ -1740,6 +1746,8 @@ namespace ts { | ||
| StringLike = String | StringLiteral, | ||
| NumberLike = Number | Enum, | ||
| ObjectType = Class | Interface | Reference | Tuple | Anonymous, | ||
| UnionOrIntersection = Union | Intersection, | ||
| StructuredType = ObjectType | Union | Intersection, | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment explaining why these are considered structured? | ||
| /* @internal */ | ||
| RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral | ||
| } | ||
| @@ -1799,17 +1807,21 @@ namespace ts { | ||
| baseArrayType: TypeReference; // Array<T> where T is best common type of element types | ||
| } | ||
| export interface UnionType extends Type { | ||
| export interface UnionOrIntersectionType extends Type { | ||
| types: Type[]; // Constituent types | ||
| /* @internal */ | ||
| reducedType: Type; // Reduced union type (all subtypes removed) | ||
| /* @internal */ | ||
| resolvedProperties: SymbolTable; // Cache of resolved properties | ||
| } | ||
| export interface UnionType extends UnionOrIntersectionType { } | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add brands. | ||
| export interface IntersectionType extends UnionOrIntersectionType { } | ||
| /* @internal */ | ||
| // Resolved objector union type | ||
| export interface ResolvedType extends ObjectType, UnionType { | ||
| // Resolved object, union, or intersection type | ||
| export interface ResolvedType extends ObjectType, UnionOrIntersectionType { | ||
| members: SymbolTable; // Properties by name | ||
| properties: Symbol[]; // Properties | ||
| callSignatures: Signature[]; // Call signatures of type | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| //// [contextualIntersectionType.ts] | ||
| var x: { a: (s: string) => string } & { b: (n: number) => number }; | ||
| x = { | ||
| a: s => s, | ||
| b: n => n | ||
| }; | ||
| //// [contextualIntersectionType.js] | ||
| var x; | ||
| x = { | ||
| a: function (s) { return s; }, | ||
| b: function (n) { return n; } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| === tests/cases/conformance/types/intersection/contextualIntersectionType.ts === | ||
| var x: { a: (s: string) => string } & { b: (n: number) => number }; | ||
| >x : Symbol(x, Decl(contextualIntersectionType.ts, 0, 3)) | ||
| >a : Symbol(a, Decl(contextualIntersectionType.ts, 0, 8)) | ||
| >s : Symbol(s, Decl(contextualIntersectionType.ts, 0, 13)) | ||
| >b : Symbol(b, Decl(contextualIntersectionType.ts, 0, 39)) | ||
| >n : Symbol(n, Decl(contextualIntersectionType.ts, 0, 44)) | ||
| x = { | ||
| >x : Symbol(x, Decl(contextualIntersectionType.ts, 0, 3)) | ||
| a: s => s, | ||
| >a : Symbol(a, Decl(contextualIntersectionType.ts, 1, 5)) | ||
| >s : Symbol(s, Decl(contextualIntersectionType.ts, 2, 6)) | ||
| >s : Symbol(s, Decl(contextualIntersectionType.ts, 2, 6)) | ||
| b: n => n | ||
| >b : Symbol(b, Decl(contextualIntersectionType.ts, 2, 14)) | ||
| >n : Symbol(n, Decl(contextualIntersectionType.ts, 3, 6)) | ||
| >n : Symbol(n, Decl(contextualIntersectionType.ts, 3, 6)) | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| === tests/cases/conformance/types/intersection/contextualIntersectionType.ts === | ||
| var x: { a: (s: string) => string } & { b: (n: number) => number }; | ||
| >x : { a: (s: string) => string; } & { b: (n: number) => number; } | ||
| >a : (s: string) => string | ||
| >s : string | ||
| >b : (n: number) => number | ||
| >n : number | ||
| x = { | ||
| >x = { a: s => s, b: n => n} : { a: (s: string) => string; b: (n: number) => number; } | ||
| >x : { a: (s: string) => string; } & { b: (n: number) => number; } | ||
| >{ a: s => s, b: n => n} : { a: (s: string) => string; b: (n: number) => number; } | ||
| a: s => s, | ||
| >a : (s: string) => string | ||
| >s => s : (s: string) => string | ||
| >s : string | ||
| >s : string | ||
| b: n => n | ||
| >b : (n: number) => number | ||
| >n => n : (n: number) => number | ||
| >n : number | ||
| >n : number | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice reuse!