Skip to content

Commit 1648d0c

Browse files
committed
Minor fixes
1 parent d61a693 commit 1648d0c

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9877,7 +9877,7 @@ namespace ts {
98779877
return false;
98789878
}
98799879

9880-
function getPropertyTypeForIndexType(originalObjectType: Type, objectType: Type, indexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression | undefined, cacheSymbol: boolean) {
9880+
function getPropertyTypeForIndexType(originalObjectType: Type, objectType: Type, indexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression | undefined, writing: boolean, cacheSymbol: boolean) {
98819881
const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
98829882
const propName = isTypeUsableAsPropertyName(indexType) ?
98839883
getPropertyNameFromType(indexType) :
@@ -9928,7 +9928,7 @@ namespace ts {
99289928
if (indexInfo) {
99299929
const isAssignment = accessExpression && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression));
99309930
if (isAssignment && maybeTypeOfKind(originalObjectType, TypeFlags.Instantiable)) {
9931-
error(accessExpression, Diagnostics.Type_0_cannot_be_indexed_by_type_1, typeToString(originalObjectType), typeToString(indexType));
9931+
error(accessExpression, Diagnostics.Type_0_cannot_be_used_to_index_type_1, typeToString(indexType), typeToString(originalObjectType));
99329932
return undefined;
99339933
}
99349934
if (accessNode && !isTypeAssignableToKind(indexType, TypeFlags.String | TypeFlags.Number)) {
@@ -9938,6 +9938,9 @@ namespace ts {
99389938
else if (isAssignment && indexInfo.isReadonly) {
99399939
error(accessExpression, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
99409940
}
9941+
else if (writing && indexInfo.isReadonly) {
9942+
return undefined;
9943+
}
99419944
return indexInfo.type;
99429945
}
99439946
if (indexType.flags & TypeFlags.Never) {
@@ -10081,11 +10084,11 @@ namespace ts {
1008110084
return instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper);
1008210085
}
1008310086

10084-
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, writing?: boolean): Type {
10085-
return getIndexedAccessTypeOrUndefined(objectType, indexType, accessNode, writing) || (accessNode ? errorType : unknownType);
10087+
function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression): Type {
10088+
return getIndexedAccessTypeOrUndefined(objectType, indexType, accessNode, /*writing*/ false) || (accessNode ? errorType : unknownType);
1008610089
}
1008710090

10088-
function getIndexedAccessTypeOrUndefined(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, writing?: boolean): Type | undefined {
10091+
function getIndexedAccessTypeOrUndefined(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression, writing = false): Type | undefined {
1008910092
if (objectType === wildcardType || indexType === wildcardType) {
1009010093
return wildcardType;
1009110094
}
@@ -10114,7 +10117,7 @@ namespace ts {
1011410117
const propTypes: Type[] = [];
1011510118
let wasMissingProp = false;
1011610119
for (const t of (<UnionType>indexType).types) {
10117-
const propType = getPropertyTypeForIndexType(objectType, apparentObjectType, t, accessNode, /*cacheSymbol*/ false);
10120+
const propType = getPropertyTypeForIndexType(objectType, apparentObjectType, t, accessNode, writing, /*cacheSymbol*/ false);
1011810121
if (propType) {
1011910122
propTypes.push(propType);
1012010123
}
@@ -10132,7 +10135,7 @@ namespace ts {
1013210135
}
1013310136
return writing ? getIntersectionType(propTypes) : getUnionType(propTypes);
1013410137
}
10135-
return getPropertyTypeForIndexType(objectType, apparentObjectType, indexType, accessNode, /*cacheSymbol*/ true);
10138+
return getPropertyTypeForIndexType(objectType, apparentObjectType, indexType, accessNode, writing, /*cacheSymbol*/ true);
1013610139
}
1013710140

1013810141
function getTypeFromIndexedAccessTypeNode(node: IndexedAccessTypeNode) {
@@ -12828,17 +12831,17 @@ namespace ts {
1282812831
if (indexType.flags & TypeFlags.StructuredOrInstantiable) {
1282912832
const keyType = getLowerBoundOfKeyType(indexType, /*isIndexType*/ true);
1283012833
if (keyType !== indexType && !(keyType.flags & TypeFlags.Never)) {
12831-
const targetType = getIndexedAccessType(objectType, keyType, /*accessNode*/ undefined, /*writing*/ true);
12832-
if (result = isRelatedTo(source, targetType, reportErrors)) {
12834+
const targetType = getIndexedAccessTypeOrUndefined(objectType, keyType, /*accessNode*/ undefined, /*writing*/ true);
12835+
if (targetType && (result = isRelatedTo(source, targetType, reportErrors))) {
1283312836
return result;
1283412837
}
1283512838
}
1283612839
}
1283712840
else {
1283812841
const constraint = getConstraintOfType(objectType);
1283912842
if (constraint) {
12840-
const targetType = getIndexedAccessType(constraint, indexType, /*accessNode*/ undefined, /*writing*/ true);
12841-
if (result = isRelatedTo(source, targetType, reportErrors)) {
12843+
const targetType = getIndexedAccessTypeOrUndefined(constraint, indexType, /*accessNode*/ undefined, /*writing*/ true);
12844+
if (targetType && (result = isRelatedTo(source, targetType, reportErrors))) {
1284212845
return result;
1284312846
}
1284412847
}
@@ -19986,7 +19989,7 @@ namespace ts {
1998619989
}
1998719990

1998819991
const effectiveIndexType = isForInVariableForNumericPropertyNames(indexExpression) ? numberType : indexType;
19989-
const indexedAccessType = getIndexedAccessType(objectType, effectiveIndexType, node, isAssignmentTarget(node));
19992+
const indexedAccessType = getIndexedAccessTypeOrUndefined(objectType, effectiveIndexType, node, isAssignmentTarget(node)) || errorType;
1999019993
return checkIndexedAccessIndexType(indexedAccessType, node);
1999119994
}
1999219995

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,10 +2152,6 @@
21522152
"category": "Error",
21532153
"code": 2593
21542154
},
2155-
"Type '{0}' cannot be indexed by type '{1}'.": {
2156-
"category": "Error",
2157-
"code": 2594
2158-
},
21592155
"JSX element attributes type '{0}' may not be a union type.": {
21602156
"category": "Error",
21612157
"code": 2600

0 commit comments

Comments
 (0)