Error when writing to readonly tuple in rest element range

This commit is contained in:
Andrew Branch
2019-05-22 15:03:17 -07:00
parent eecb6d9049
commit 9f6791a5ab
6 changed files with 145 additions and 13 deletions

View File

@@ -10101,6 +10101,7 @@ namespace ts {
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
}
}
errorIfWritingToReadonlyIndex(getIndexInfoOfType(objectType, IndexKind.Number));
return mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
}
}
@@ -10122,13 +10123,7 @@ namespace ts {
error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType));
return indexInfo.type;
}
if (indexInfo.isReadonly && accessExpression && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression))) {
if (accessExpression) {
error(accessExpression, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
return indexInfo.type;
}
return undefined;
}
errorIfWritingToReadonlyIndex(indexInfo);
return indexInfo.type;
}
if (indexType.flags & TypeFlags.Never) {
@@ -10188,6 +10183,12 @@ namespace ts {
return indexType;
}
return undefined;
function errorIfWritingToReadonlyIndex(indexInfo: IndexInfo | undefined): void {
if (indexInfo && indexInfo.isReadonly && accessExpression && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression))) {
error(accessExpression, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
}
}
}
function getIndexNodeForAccessExpression(accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression) {