Merge pull request #31542 from andrewbranch/bug/31481

Error when writing to rest element range of readonly tuple
This commit is contained in:
Andrew Branch
2019-05-23 13:38:09 -07:00
committed by GitHub
6 changed files with 147 additions and 8 deletions

View File

@@ -10106,6 +10106,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);
}
}
@@ -10127,13 +10128,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) {
@@ -10193,6 +10188,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) {