-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Unions and intersections of readonly properties are now also readonly #9167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sandersn
merged 3 commits into
master
from
make-unions-and-intersections-of-readonly-properties-readonly
Jun 24, 2016
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/intersectionTypeReadonly.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
tests/cases/conformance/types/intersection/intersectionTypeReadonly.ts(17,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
tests/cases/conformance/types/intersection/intersectionTypeReadonly.ts(19,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
tests/cases/conformance/types/intersection/intersectionTypeReadonly.ts(21,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
tests/cases/conformance/types/intersection/intersectionTypeReadonly.ts(23,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
tests/cases/conformance/types/intersection/intersectionTypeReadonly.ts(25,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
|
||
|
||
==== tests/cases/conformance/types/intersection/intersectionTypeReadonly.ts (5 errors) ==== | ||
interface Base { | ||
readonly value: number; | ||
} | ||
interface Identical { | ||
readonly value: number; | ||
} | ||
interface Mutable { | ||
value: number; | ||
} | ||
interface DifferentType { | ||
readonly value: string; | ||
} | ||
interface DifferentName { | ||
readonly other: number; | ||
} | ||
let base: Base; | ||
base.value = 12 // error, lhs can't be a readonly property | ||
~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
let identical: Base & Identical; | ||
identical.value = 12; // error, lhs can't be a readonly property | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
let mutable: Base & Mutable; | ||
mutable.value = 12; // error, lhs can't be a readonly property | ||
~~~~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
let differentType: Base & DifferentType; | ||
differentType.value = 12; // error, lhs can't be a readonly property | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
let differentName: Base & DifferentName; | ||
differentName.value = 12; // error, property 'value' doesn't exist | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//// [intersectionTypeReadonly.ts] | ||
interface Base { | ||
readonly value: number; | ||
} | ||
interface Identical { | ||
readonly value: number; | ||
} | ||
interface Mutable { | ||
value: number; | ||
} | ||
interface DifferentType { | ||
readonly value: string; | ||
} | ||
interface DifferentName { | ||
readonly other: number; | ||
} | ||
let base: Base; | ||
base.value = 12 // error, lhs can't be a readonly property | ||
let identical: Base & Identical; | ||
identical.value = 12; // error, lhs can't be a readonly property | ||
let mutable: Base & Mutable; | ||
mutable.value = 12; // error, lhs can't be a readonly property | ||
let differentType: Base & DifferentType; | ||
differentType.value = 12; // error, lhs can't be a readonly property | ||
let differentName: Base & DifferentName; | ||
differentName.value = 12; // error, property 'value' doesn't exist | ||
|
||
|
||
//// [intersectionTypeReadonly.js] | ||
var base; | ||
base.value = 12; // error, lhs can't be a readonly property | ||
var identical; | ||
identical.value = 12; // error, lhs can't be a readonly property | ||
var mutable; | ||
mutable.value = 12; // error, lhs can't be a readonly property | ||
var differentType; | ||
differentType.value = 12; // error, lhs can't be a readonly property | ||
var differentName; | ||
differentName.value = 12; // error, property 'value' doesn't exist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
tests/cases/conformance/types/union/unionTypeReadonly.ts(17,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
tests/cases/conformance/types/union/unionTypeReadonly.ts(19,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
tests/cases/conformance/types/union/unionTypeReadonly.ts(21,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
tests/cases/conformance/types/union/unionTypeReadonly.ts(23,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
tests/cases/conformance/types/union/unionTypeReadonly.ts(25,15): error TS2339: Property 'value' does not exist on type 'Base | DifferentName'. | ||
|
||
|
||
==== tests/cases/conformance/types/union/unionTypeReadonly.ts (5 errors) ==== | ||
interface Base { | ||
readonly value: number; | ||
} | ||
interface Identical { | ||
readonly value: number; | ||
} | ||
interface Mutable { | ||
value: number; | ||
} | ||
interface DifferentType { | ||
readonly value: string; | ||
} | ||
interface DifferentName { | ||
readonly other: number; | ||
} | ||
let base: Base; | ||
base.value = 12 // error, lhs can't be a readonly property | ||
~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
let identical: Base | Identical; | ||
identical.value = 12; // error, lhs can't be a readonly property | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
let mutable: Base | Mutable; | ||
mutable.value = 12; // error, lhs can't be a readonly property | ||
~~~~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
let differentType: Base | DifferentType; | ||
differentType.value = 12; // error, lhs can't be a readonly property | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. | ||
let differentName: Base | DifferentName; | ||
differentName.value = 12; // error, property 'value' doesn't exist | ||
~~~~~ | ||
!!! error TS2339: Property 'value' does not exist on type 'Base | DifferentName'. | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//// [unionTypeReadonly.ts] | ||
interface Base { | ||
readonly value: number; | ||
} | ||
interface Identical { | ||
readonly value: number; | ||
} | ||
interface Mutable { | ||
value: number; | ||
} | ||
interface DifferentType { | ||
readonly value: string; | ||
} | ||
interface DifferentName { | ||
readonly other: number; | ||
} | ||
let base: Base; | ||
base.value = 12 // error, lhs can't be a readonly property | ||
let identical: Base | Identical; | ||
identical.value = 12; // error, lhs can't be a readonly property | ||
let mutable: Base | Mutable; | ||
mutable.value = 12; // error, lhs can't be a readonly property | ||
let differentType: Base | DifferentType; | ||
differentType.value = 12; // error, lhs can't be a readonly property | ||
let differentName: Base | DifferentName; | ||
differentName.value = 12; // error, property 'value' doesn't exist | ||
|
||
|
||
|
||
//// [unionTypeReadonly.js] | ||
var base; | ||
base.value = 12; // error, lhs can't be a readonly property | ||
var identical; | ||
identical.value = 12; // error, lhs can't be a readonly property | ||
var mutable; | ||
mutable.value = 12; // error, lhs can't be a readonly property | ||
var differentType; | ||
differentType.value = 12; // error, lhs can't be a readonly property | ||
var differentName; | ||
differentName.value = 12; // error, property 'value' doesn't exist |
25 changes: 25 additions & 0 deletions
25
tests/cases/conformance/types/intersection/intersectionTypeReadonly.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
interface Base { | ||
readonly value: number; | ||
} | ||
interface Identical { | ||
readonly value: number; | ||
} | ||
interface Mutable { | ||
value: number; | ||
} | ||
interface DifferentType { | ||
readonly value: string; | ||
} | ||
interface DifferentName { | ||
readonly other: number; | ||
} | ||
let base: Base; | ||
base.value = 12 // error, lhs can't be a readonly property | ||
let identical: Base & Identical; | ||
identical.value = 12; // error, lhs can't be a readonly property | ||
let mutable: Base & Mutable; | ||
mutable.value = 12; // error, lhs can't be a readonly property | ||
let differentType: Base & DifferentType; | ||
differentType.value = 12; // error, lhs can't be a readonly property | ||
let differentName: Base & DifferentName; | ||
differentName.value = 12; // error, property 'value' doesn't exist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
interface Base { | ||
readonly value: number; | ||
} | ||
interface Identical { | ||
readonly value: number; | ||
} | ||
interface Mutable { | ||
value: number; | ||
} | ||
interface DifferentType { | ||
readonly value: string; | ||
} | ||
interface DifferentName { | ||
readonly other: number; | ||
} | ||
let base: Base; | ||
base.value = 12 // error, lhs can't be a readonly property | ||
let identical: Base | Identical; | ||
identical.value = 12; // error, lhs can't be a readonly property | ||
let mutable: Base | Mutable; | ||
mutable.value = 12; // error, lhs can't be a readonly property | ||
let differentType: Base | DifferentType; | ||
differentType.value = 12; // error, lhs can't be a readonly property | ||
let differentName: Base | DifferentName; | ||
differentName.value = 12; // error, property 'value' doesn't exist | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could probably be more efficient. When a symbol has multiple declarations (such as an overloaded method) we'll end up doing the same check multiple times even though the outcome is always false. Ideally we'd have a
SymbolFlags.Readonly
that we compute in the same way asSymbolFlags.Optional
, but we're out of flag bits. Perhaps it would be better to compute the readonly state increateUnionOrIntersectionProperty
(by callingisReadonlySymbol
for each underlying symbol) and then record the result in anisReadonly
property on the symbol itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a good idea. Done.