Skip to content

Commit a9af10b

Browse files
committed
Intersections as their own 'this' type
1 parent f9a65e4 commit a9af10b

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,10 +4326,13 @@ namespace ts {
43264326

43274327
function getTypeWithThisArgument(type: Type, thisArgument?: Type): Type {
43284328
if (getObjectFlags(type) & ObjectFlags.Reference) {
4329-
return createTypeReference((<TypeReference>type).target,
4330-
concatenate((<TypeReference>type).typeArguments, [thisArgument || (<TypeReference>type).target.thisType]));
4329+
const target = (<TypeReference>type).target;
4330+
const typeArguments = (<TypeReference>type).typeArguments;
4331+
if (length(target.typeParameters) === length(typeArguments)) {
4332+
return createTypeReference(target, concatenate(typeArguments, [thisArgument || target.thisType]));
4333+
}
43314334
}
4332-
if (type.flags & TypeFlags.Intersection) {
4335+
else if (type.flags & TypeFlags.Intersection) {
43334336
return getIntersectionType(map((<IntersectionType>type).types, t => getTypeWithThisArgument(t, thisArgument)));
43344337
}
43354338
return type;
@@ -4858,14 +4861,19 @@ namespace ts {
48584861
}
48594862
}
48604863

4864+
function getApparentTypeOfIntersectionType(type: IntersectionType) {
4865+
return type.resolvedIndexType || (type.resolvedApparentType = getTypeWithThisArgument(type, type));
4866+
}
4867+
48614868
/**
48624869
* For a type parameter, return the base constraint of the type parameter. For the string, number,
48634870
* boolean, and symbol primitive types, return the corresponding object types. Otherwise return the
48644871
* type itself. Note that the apparent type of a union type is the union type itself.
48654872
*/
48664873
function getApparentType(type: Type): Type {
48674874
const t = type.flags & TypeFlags.TypeVariable ? getBaseConstraintOfType(<TypeVariable>type) || emptyObjectType : type;
4868-
return t.flags & TypeFlags.StringLike ? globalStringType :
4875+
return t.flags & TypeFlags.Intersection ? getApparentTypeOfIntersectionType(<IntersectionType>type) :
4876+
t.flags & TypeFlags.StringLike ? globalStringType :
48694877
t.flags & TypeFlags.NumberLike ? globalNumberType :
48704878
t.flags & TypeFlags.BooleanLike ? globalBooleanType :
48714879
t.flags & TypeFlags.ESSymbol ? getGlobalESSymbolType() :

src/compiler/core.ts

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

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

src/compiler/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2961,7 +2961,10 @@
29612961

29622962
export interface UnionType extends UnionOrIntersectionType { }
29632963

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

29662969
export type StructuredType = ObjectType | UnionType | IntersectionType;
29672970

0 commit comments

Comments
 (0)