Skip to content

Commit 25cb02e

Browse files
committed
Fix circularity check, simplify default type mapper
1 parent 442f540 commit 25cb02e

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/compiler/checker.ts

+18-24
Original file line numberDiff line numberDiff line change
@@ -4842,8 +4842,6 @@ namespace ts {
48424842

48434843
function getDefaultOfTypeParameter(typeParameter: TypeParameter): Type {
48444844
return hasNonCircularDefault(typeParameter) ? getDefaultFromTypeParameter(typeParameter) : undefined;
4845-
// const defaultType = getResolvedDefault(typeParameter);
4846-
// return defaultType !== noConstraintOrDefaultType && defaultType !== circularConstraintOrDefaultType ? defaultType : undefined;
48474845
}
48484846

48494847
function hasNonCircularDefault(type: TypeParameter) {
@@ -4870,11 +4868,9 @@ namespace ts {
48704868
return getResolvedDefault(<TypeParameter>type);
48714869
}
48724870
if (type.flags & TypeFlags.UnionOrIntersection) {
4873-
const types = (<UnionOrIntersectionType>type).types;
4874-
const defaultTypes = filter(map(types, getResolvedDefaultWorker), x => x !== circularConstraintOrDefaultType);
4875-
return type.flags & TypeFlags.Union && defaultTypes.length === types.length ? getUnionType(defaultTypes) :
4876-
type.flags & TypeFlags.Intersection && defaultTypes.length ? getIntersectionType(defaultTypes) :
4877-
undefined;
4871+
const types = map((<UnionOrIntersectionType>type).types, getResolvedDefaultWorker);
4872+
return some(types, x => x === circularConstraintOrDefaultType) ? circularConstraintOrDefaultType :
4873+
type.flags & TypeFlags.Union ? getUnionType(types) : getIntersectionType(types);
48784874
}
48794875
return type;
48804876
}
@@ -5178,30 +5174,28 @@ namespace ts {
51785174
}
51795175

51805176
function fillMissingTypeArguments(typeArguments: Type[] | undefined, typeParameters: TypeParameter[] | undefined, minTypeArgumentCount: number) {
5181-
const numTypeArguments = typeArguments ? typeArguments.length : 0;
51825177
const numTypeParameters = typeParameters ? typeParameters.length : 0;
5183-
if (numTypeArguments >= minTypeArgumentCount && numTypeArguments <= numTypeParameters) {
5184-
if (numTypeParameters) {
5178+
if (numTypeParameters) {
5179+
const numTypeArguments = typeArguments ? typeArguments.length : 0;
5180+
if (numTypeArguments >= minTypeArgumentCount && numTypeArguments <= numTypeParameters) {
51855181
if (!typeArguments) {
51865182
typeArguments = [];
51875183
}
5184+
5185+
// Map an unsatisfied type parameter with a default type to the default type.
5186+
// If a type parameter does not have a default type, or if the default type
5187+
// is a circular reference, the empty object type is used.
51885188
const mapper: TypeMapper = t => {
5189-
for (let i = 0; i < numTypeParameters; i++) {
5190-
if (t === typeParameters[i]) {
5191-
if (!typeArguments[i]) {
5192-
typeArguments[i] = emptyObjectType;
5193-
const defaultType = getDefaultOfTypeParameter(typeParameters[i]);
5194-
if (defaultType) {
5195-
typeArguments[i] = instantiateType(defaultType, mapper);
5196-
}
5197-
}
5198-
return typeArguments[i];
5199-
}
5200-
}
5201-
return t;
5189+
const i = indexOf(typeParameters, t);
5190+
return i >= 0
5191+
? typeArguments[i] || (typeArguments[i] =
5192+
instantiateType(getDefaultOfTypeParameter(typeParameters[i]), mapper) ||
5193+
emptyObjectType)
5194+
: t;
52025195
};
5196+
52035197
for (let i = numTypeArguments; i < numTypeParameters; i++) {
5204-
typeArguments[i] = instantiateType(typeParameters[i], mapper);
5198+
instantiateType(typeParameters[i], mapper);
52055199
}
52065200
}
52075201
}

0 commit comments

Comments
 (0)