Skip to content

Commit 3d8a9c8

Browse files
committed
Less aggressive check for index signatures
1 parent b1f746b commit 3d8a9c8

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16031,11 +16031,6 @@ namespace ts {
1603116031
return related;
1603216032
}
1603316033

16034-
function hasIndexInfo(type: Type, kind: IndexKind): boolean {
16035-
return type.flags & TypeFlags.Intersection ? every((<IntersectionType>type).types, t => hasIndexInfo(t, kind)) :
16036-
!!(getIndexInfoOfType(type, kind) || kind === IndexKind.Number && getIndexInfoOfType(type, IndexKind.String) || isObjectTypeWithInferableIndex(type));
16037-
}
16038-
1603916034
function indexTypesRelatedTo(source: Type, target: Type, kind: IndexKind, sourceIsPrimitive: boolean, reportErrors: boolean): Ternary {
1604016035
if (relation === identityRelation) {
1604116036
return indexTypesIdenticalTo(source, target, kind);
@@ -16050,24 +16045,24 @@ namespace ts {
1605016045
// if T is related to U.
1605116046
return kind === IndexKind.String ? isRelatedTo(getTemplateTypeFromMappedType(source), targetType, reportErrors) : Ternary.False;
1605216047
}
16053-
if (!hasIndexInfo(source, kind)) {
16054-
if (reportErrors) {
16055-
reportError(Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source));
16056-
}
16057-
return Ternary.False;
16058-
}
1605916048
const indexType = getIndexTypeOfType(source, kind) || kind === IndexKind.Number && getIndexTypeOfType(source, IndexKind.String);
1606016049
if (indexType) {
1606116050
return indexTypeRelatedTo(indexType, targetType, reportErrors);
1606216051
}
16063-
let related = eachPropertyRelatedTo(source, targetType, kind, reportErrors);
16064-
if (related && kind === IndexKind.String) {
16065-
const numberIndexType = getIndexTypeOfType(source, IndexKind.Number);
16066-
if (numberIndexType) {
16067-
related &= indexTypeRelatedTo(numberIndexType, targetType, reportErrors);
16052+
if (isObjectTypeWithInferableIndex(source)) {
16053+
let related = eachPropertyRelatedTo(source, targetType, kind, reportErrors);
16054+
if (related && kind === IndexKind.String) {
16055+
const numberIndexType = getIndexTypeOfType(source, IndexKind.Number);
16056+
if (numberIndexType) {
16057+
related &= indexTypeRelatedTo(numberIndexType, targetType, reportErrors);
16058+
}
1606816059
}
16060+
return related;
1606916061
}
16070-
return related;
16062+
if (reportErrors) {
16063+
reportError(Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source));
16064+
}
16065+
return Ternary.False;
1607116066
}
1607216067

1607316068
function indexTypesIdenticalTo(source: Type, target: Type, indexKind: IndexKind): Ternary {
@@ -16820,7 +16815,8 @@ namespace ts {
1682016815
* with no call or construct signatures.
1682116816
*/
1682216817
function isObjectTypeWithInferableIndex(type: Type): boolean {
16823-
return !!(type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.Enum | SymbolFlags.ValueModule)) !== 0 &&
16818+
return type.flags & TypeFlags.Intersection ? every((<IntersectionType>type).types, isObjectTypeWithInferableIndex) :
16819+
!!(type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.Enum | SymbolFlags.ValueModule)) !== 0 &&
1682416820
!typeHasCallOrConstructSignatures(type)) || !!(getObjectFlags(type) & ObjectFlags.ReverseMapped && isObjectTypeWithInferableIndex((type as ReverseMappedType).source));
1682516821
}
1682616822

0 commit comments

Comments
 (0)