Skip to content

Commit a8160de

Browse files
committed
Empty array literal has a non-inferrable element type
1 parent 8d5b052 commit a8160de

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ namespace ts {
281281
const voidType = createIntrinsicType(TypeFlags.Void, "void");
282282
const neverType = createIntrinsicType(TypeFlags.Never, "never");
283283
const silentNeverType = createIntrinsicType(TypeFlags.Never, "never");
284+
const implicitNeverType = createIntrinsicType(TypeFlags.Never, "never");
284285
const nonPrimitiveType = createIntrinsicType(TypeFlags.NonPrimitive, "object");
285286

286287
const emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
@@ -7684,7 +7685,7 @@ namespace ts {
76847685

76857686
function getIndexTypeOrString(type: Type): Type {
76867687
const indexType = getIndexType(type);
7687-
return indexType !== neverType ? indexType : stringType;
7688+
return indexType.flags & TypeFlags.Never ? stringType : indexType;
76887689
}
76897690

76907691
function getTypeFromTypeOperatorNode(node: TypeOperatorNode) {
@@ -8861,8 +8862,8 @@ namespace ts {
88618862
function isSimpleTypeRelatedTo(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorReporter?: ErrorReporter) {
88628863
const s = source.flags;
88638864
const t = target.flags;
8864-
if (t & TypeFlags.Never) return false;
88658865
if (t & TypeFlags.Any || s & TypeFlags.Never) return true;
8866+
if (t & TypeFlags.Never) return false;
88668867
if (s & TypeFlags.StringLike && t & TypeFlags.String) return true;
88678868
if (s & TypeFlags.StringLiteral && s & TypeFlags.EnumLiteral &&
88688869
t & TypeFlags.StringLiteral && !(t & TypeFlags.EnumLiteral) &&
@@ -10323,7 +10324,7 @@ namespace ts {
1032310324

1032410325
function isEmptyArrayLiteralType(type: Type): boolean {
1032510326
const elementType = isArrayType(type) ? (<TypeReference>type).typeArguments[0] : undefined;
10326-
return elementType === undefinedWideningType || elementType === neverType;
10327+
return elementType === undefinedWideningType || elementType === implicitNeverType;
1032710328
}
1032810329

1032910330
function isTupleLikeType(type: Type): boolean {
@@ -10880,9 +10881,10 @@ namespace ts {
1088010881
// Because the anyFunctionType is internal, it should not be exposed to the user by adding
1088110882
// it as an inference candidate. Hopefully, a better candidate will come along that does
1088210883
// not contain anyFunctionType when we come back to this argument for its second round
10883-
// of inference. Also, we exclude inferences for silentNeverType which is used as a wildcard
10884-
// when constructing types from type parameters that had no inference candidates.
10885-
if (source.flags & TypeFlags.ContainsAnyFunctionType || source === silentNeverType) {
10884+
// of inference. Also, we exclude inferences for silentNeverType (which is used as a wildcard
10885+
// when constructing types from type parameters that had no inference candidates) and
10886+
// implicitNeverType (which is used as the element type for empty array literals).
10887+
if (source.flags & TypeFlags.ContainsAnyFunctionType || source === silentNeverType || source === implicitNeverType) {
1088610888
return;
1088710889
}
1088810890
const inference = getInferenceInfoForType(target);
@@ -13923,7 +13925,7 @@ namespace ts {
1392313925
}
1392413926
return createArrayType(elementTypes.length ?
1392513927
getUnionType(elementTypes, /*subtypeReduction*/ true) :
13926-
strictNullChecks ? neverType : undefinedWideningType);
13928+
strictNullChecks ? implicitNeverType : undefinedWideningType);
1392713929
}
1392813930

1392913931
function isNumericName(name: DeclarationName): boolean {

0 commit comments

Comments
 (0)