Skip to content

Commit 5b9004e

Browse files
authored
Merge pull request #13604 from Microsoft/intersectionBaseTypes
Allow deriving from object and intersection types
2 parents 6c90e3f + 3a34cb3 commit 5b9004e

17 files changed

+1474
-62
lines changed

src/compiler/checker.ts

Lines changed: 71 additions & 49 deletions
Large diffs are not rendered by default.

src/compiler/core.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ namespace ts {
204204
GreaterThan = 1
205205
}
206206

207+
export function length(array: any[]) {
208+
return array ? array.length : 0;
209+
}
210+
207211
/**
208212
* Iterates through 'array' by index and performs the callback on each element of array until the callback
209213
* returns a truthy value, then returns that value.

src/compiler/types.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@
23582358
getIndexInfoOfType(type: Type, kind: IndexKind): IndexInfo;
23592359
getSignaturesOfType(type: Type, kind: SignatureKind): Signature[];
23602360
getIndexTypeOfType(type: Type, kind: IndexKind): Type;
2361-
getBaseTypes(type: InterfaceType): ObjectType[];
2361+
getBaseTypes(type: InterfaceType): BaseType[];
23622362
getReturnTypeOfSignature(signature: Signature): Type;
23632363
/**
23642364
* Gets the type of a parameter at a given position in a signature.
@@ -2910,9 +2910,12 @@
29102910
/* @internal */
29112911
resolvedBaseConstructorType?: Type; // Resolved base constructor type of class
29122912
/* @internal */
2913-
resolvedBaseTypes: ObjectType[]; // Resolved base types
2913+
resolvedBaseTypes: BaseType[]; // Resolved base types
29142914
}
29152915

2916+
// Object type or intersection of object types
2917+
export type BaseType = ObjectType | IntersectionType;
2918+
29162919
export interface InterfaceTypeWithDeclaredMembers extends InterfaceType {
29172920
declaredProperties: Symbol[]; // Declared members
29182921
declaredCallSignatures: Signature[]; // Declared call signatures
@@ -2945,7 +2948,9 @@
29452948
export interface UnionOrIntersectionType extends Type {
29462949
types: Type[]; // Constituent types
29472950
/* @internal */
2948-
resolvedProperties: SymbolTable; // Cache of resolved properties
2951+
propertyCache: SymbolTable; // Cache of resolved properties
2952+
/* @internal */
2953+
resolvedProperties: Symbol[];
29492954
/* @internal */
29502955
resolvedIndexType: IndexType;
29512956
/* @internal */
@@ -2956,7 +2961,10 @@
29562961

29572962
export interface UnionType extends UnionOrIntersectionType { }
29582963

2959-
export interface IntersectionType extends UnionOrIntersectionType { }
2964+
export interface IntersectionType extends UnionOrIntersectionType {
2965+
/* @internal */
2966+
resolvedApparentType: Type;
2967+
}
29602968

29612969
export type StructuredType = ObjectType | UnionType | IntersectionType;
29622970

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ namespace ts {
382382
getNumberIndexType(): Type {
383383
return this.checker.getIndexTypeOfType(this, IndexKind.Number);
384384
}
385-
getBaseTypes(): ObjectType[] {
385+
getBaseTypes(): BaseType[] {
386386
return this.flags & TypeFlags.Object && this.objectFlags & (ObjectFlags.Class | ObjectFlags.Interface)
387387
? this.checker.getBaseTypes(<InterfaceType><Type>this)
388388
: undefined;

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace ts {
3333
getConstructSignatures(): Signature[];
3434
getStringIndexType(): Type;
3535
getNumberIndexType(): Type;
36-
getBaseTypes(): ObjectType[];
36+
getBaseTypes(): BaseType[];
3737
getNonNullableType(): Type;
3838
}
3939

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//// [interfaceExtendsObjectIntersection.ts]
2+
3+
type T1 = { a: number };
4+
type T2 = T1 & { b: number };
5+
type T3 = () => void;
6+
type T4 = new () => { a: number };
7+
type T5 = number[];
8+
type T6 = [string, number];
9+
type T7 = { [P in 'a' | 'b' | 'c']: string };
10+
11+
interface I1 extends T1 { x: string }
12+
interface I2 extends T2 { x: string }
13+
interface I3 extends T3 { x: string }
14+
interface I4 extends T4 { x: string }
15+
interface I5 extends T5 { x: string }
16+
interface I6 extends T6 { x: string }
17+
interface I7 extends T7 { x: string }
18+
19+
type Constructor<T> = new () => T;
20+
declare function Constructor<T>(): Constructor<T>;
21+
22+
class C1 extends Constructor<I1>() { x: string }
23+
class C2 extends Constructor<I2>() { x: string }
24+
class C3 extends Constructor<I3>() { x: string }
25+
class C4 extends Constructor<I4>() { x: string }
26+
class C5 extends Constructor<I5>() { x: string }
27+
class C6 extends Constructor<I6>() { x: string }
28+
class C7 extends Constructor<I7>() { x: string }
29+
30+
declare function fx(x: string): string;
31+
declare class CX { a: number }
32+
declare enum EX { A, B, C }
33+
declare namespace NX { export const a = 1 }
34+
35+
type T10 = typeof fx;
36+
type T11 = typeof CX;
37+
type T12 = typeof EX;
38+
type T13 = typeof NX;
39+
40+
interface I10 extends T10 { x: string }
41+
interface I11 extends T11 { x: string }
42+
interface I12 extends T12 { x: string }
43+
interface I13 extends T13 { x: string }
44+
45+
type Identifiable<T> = { _id: string } & T;
46+
47+
interface I20 extends Partial<T1> { x: string }
48+
interface I21 extends Readonly<T1> { x: string }
49+
interface I22 extends Identifiable<T1> { x: string }
50+
interface I23 extends Identifiable<T1 & { b: number}> { x: string }
51+
52+
class C20 extends Constructor<Partial<T1>>() { x: string }
53+
class C21 extends Constructor<Readonly<T1>>() { x: string }
54+
class C22 extends Constructor<Identifiable<T1>>() { x: string }
55+
class C23 extends Constructor<Identifiable<T1 & { b: number}>>() { x: string }
56+
57+
58+
//// [interfaceExtendsObjectIntersection.js]
59+
var __extends = (this && this.__extends) || (function () {
60+
var extendStatics = Object.setPrototypeOf ||
61+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
62+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
63+
return function (d, b) {
64+
extendStatics(d, b);
65+
function __() { this.constructor = d; }
66+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
67+
};
68+
})();
69+
var C1 = (function (_super) {
70+
__extends(C1, _super);
71+
function C1() {
72+
return _super !== null && _super.apply(this, arguments) || this;
73+
}
74+
return C1;
75+
}(Constructor()));
76+
var C2 = (function (_super) {
77+
__extends(C2, _super);
78+
function C2() {
79+
return _super !== null && _super.apply(this, arguments) || this;
80+
}
81+
return C2;
82+
}(Constructor()));
83+
var C3 = (function (_super) {
84+
__extends(C3, _super);
85+
function C3() {
86+
return _super !== null && _super.apply(this, arguments) || this;
87+
}
88+
return C3;
89+
}(Constructor()));
90+
var C4 = (function (_super) {
91+
__extends(C4, _super);
92+
function C4() {
93+
return _super !== null && _super.apply(this, arguments) || this;
94+
}
95+
return C4;
96+
}(Constructor()));
97+
var C5 = (function (_super) {
98+
__extends(C5, _super);
99+
function C5() {
100+
return _super !== null && _super.apply(this, arguments) || this;
101+
}
102+
return C5;
103+
}(Constructor()));
104+
var C6 = (function (_super) {
105+
__extends(C6, _super);
106+
function C6() {
107+
return _super !== null && _super.apply(this, arguments) || this;
108+
}
109+
return C6;
110+
}(Constructor()));
111+
var C7 = (function (_super) {
112+
__extends(C7, _super);
113+
function C7() {
114+
return _super !== null && _super.apply(this, arguments) || this;
115+
}
116+
return C7;
117+
}(Constructor()));
118+
var C20 = (function (_super) {
119+
__extends(C20, _super);
120+
function C20() {
121+
return _super !== null && _super.apply(this, arguments) || this;
122+
}
123+
return C20;
124+
}(Constructor()));
125+
var C21 = (function (_super) {
126+
__extends(C21, _super);
127+
function C21() {
128+
return _super !== null && _super.apply(this, arguments) || this;
129+
}
130+
return C21;
131+
}(Constructor()));
132+
var C22 = (function (_super) {
133+
__extends(C22, _super);
134+
function C22() {
135+
return _super !== null && _super.apply(this, arguments) || this;
136+
}
137+
return C22;
138+
}(Constructor()));
139+
var C23 = (function (_super) {
140+
__extends(C23, _super);
141+
function C23() {
142+
return _super !== null && _super.apply(this, arguments) || this;
143+
}
144+
return C23;
145+
}(Constructor()));

0 commit comments

Comments
 (0)