You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -12,22 +12,16 @@ To create a class, you use the `class` keyword followed by the name of the class
12
12
13
13
We'll start creating the `Album` class in a similar way to how a type or interface is created:
14
14
15
-
```typescript
15
+
```ts twoslash
16
+
// @errors: 2564
16
17
classAlbum {
17
-
title:string;// red squiggly line under title
18
-
artist:string;// red squiggly line under artist
19
-
releaseYear:number;// red squiggly line under releaseYear
18
+
title:string;
19
+
artist:string;
20
+
releaseYear:number;
20
21
}
21
22
```
22
23
23
-
At this point, even though it looks like a type or interface, TypeScript gives an error for each property in the class:
24
-
25
-
```typescript
26
-
// hovering over title shows:
27
-
28
-
// Property 'title' has no initializer and is not definitely assigned in the constructor.
29
-
```
30
-
24
+
At this point, even though it looks like a type or interface, TypeScript gives an error for each property in the class.
31
25
How do we fix this?
32
26
33
27
### Adding a Constructor
@@ -232,11 +226,15 @@ class Album {
232
226
233
227
Now if we try to access the `rating` property from outside of the class, TypeScript will give us an error:
234
228
235
-
```typescript
236
-
console.log(loopFindingJazzRecords.rating); // red squiggly line under rating
229
+
```ts twoslash
230
+
// @errors: 2341
231
+
classAlbum {
232
+
private rating =0;
233
+
}
237
234
238
-
// hovering over rating shows:
239
-
// Property 'rating' is private and only accessible within class 'Album'.
235
+
const loopFindingJazzRecords =newAlbum();
236
+
// ---cut---
237
+
console.log(loopFindingJazzRecords.rating);
240
238
```
241
239
242
240
However, this doesn't actually prevent it from being accessed at runtime - `private` is just a compile-time annotation. You could suppress the error using a `@ts-ignore` (which we'll look at later) and still access the property:
@@ -260,13 +258,28 @@ The `#` syntax behaves the same as `private`, but it's a newer feature that's pa
260
258
261
259
Attempting to access a `#`-prefixed property from outside of the class will result in a syntax error:
The `#` in front of the `x` and `y` properties means they are `readonly` and can't be modified directly outside of the class. In addition, when a getter is present without a setter, its property will also be treated as `readonly`, as seen in this test case:
778
850
779
-
```typescript
780
-
canvasNode.position= { x: 10, y: 20 }; // red squiggly line under position
851
+
```ts twoslash
852
+
// @errors: 2540
853
+
declareconst canvasNode: {
854
+
readonly position: { x:number; y:number };
855
+
};
781
856
782
-
//hovering over position shows:
783
-
// Cannot assign to 'position' because it is a read-only property.
857
+
//---cut---
858
+
canvasNode.position= { x: 10, y: 20 };
784
859
```
785
860
786
861
Your task is to write a setter for the `position` property that will allow for the test case to pass.
0 commit comments