Skip to content

Add readonly check to property access of index signature#17710

Merged
sandersn merged 3 commits into
masterfrom
add-readonly-check-to-property-access-of-index-signature
Aug 16, 2017
Merged

Add readonly check to property access of index signature#17710
sandersn merged 3 commits into
masterfrom
add-readonly-check-to-property-access-of-index-signature

Conversation

@sandersn
Copy link
Copy Markdown
Member

@sandersn sandersn commented Aug 9, 2017

Fixes #14296

Unfortunately, the checking code isn't reusable in a large chunk, so I copied it from getPropertyTypeForIndexType. It still might be better to extract the four lines to report the error into its own function.

Unfortunately, the checking code isn't reusable in a large chunk, so I copied it
from `getPropertyTypeForIndexType`. It still might be better to extract
the four lines to report the error into its own function.
Comment thread src/compiler/checker.ts Outdated
if (indexInfo && indexInfo.type) {
if (indexInfo.isReadonly && (isAssignmentTarget(node) || isDeleteTarget(node))) {
error(node, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(apparentType));
return unknownType;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return unknownType here? We know the type, even if the access is invalid.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with Ron. You should fix this in the other place where we perform a read-only check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Always happy to remove bad code! 💯

Copy link
Copy Markdown
Member

@ahejlsberg ahejlsberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with the requested changes.

Comment thread src/compiler/checker.ts Outdated
if (indexInfo && indexInfo.type) {
if (indexInfo.isReadonly && (isAssignmentTarget(node) || isDeleteTarget(node))) {
error(node, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(apparentType));
return unknownType;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with Ron. You should fix this in the other place where we perform a read-only check.

Now, in an assignment to an indexed access of a readonly property, the
resulting type is still the property's type. Previously it was
unknownType. This improves error reporting slightly by reporting some
errors that were previously missed.
@sandersn sandersn merged commit 146f828 into master Aug 16, 2017
@sandersn sandersn deleted the add-readonly-check-to-property-access-of-index-signature branch August 16, 2017 18:16
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants