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
Copy file name to clipboardExpand all lines: README.md
+71Lines changed: 71 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1199,6 +1199,77 @@ interface Config {
1199
1199
}
1200
1200
```
1201
1201
1202
+
Case of Array, you can create a read-only array by using `ReadonlyArray<T>`.
1203
+
do not allow changes such as `push()` and `fill()`, but can use features such as `concat()` and `slice()` that do not change the value.
1204
+
1205
+
**Bad:**
1206
+
1207
+
```ts
1208
+
const array:number[] = [ 1, 3, 5 ];
1209
+
array= []; // error
1210
+
array.push(100); // array will updated
1211
+
```
1212
+
1213
+
**Good:**
1214
+
1215
+
```ts
1216
+
const array:ReadonlyArray<number> = [ 1, 3, 5 ];
1217
+
array= []; // error
1218
+
array.push(100); // error
1219
+
```
1220
+
1221
+
Declaring read-only arguments in [TypeScript 3.4 is a bit easier](https://github.com/microsoft/TypeScript/wiki/What's-new-in-TypeScript#improvements-for-readonlyarray-and-readonly-tuples).
1222
+
1223
+
```ts
1224
+
function hoge(args:readonlystring[]) {
1225
+
args.push(1); // error
1226
+
}
1227
+
```
1228
+
1229
+
Prefer [const assertions](https://github.com/microsoft/TypeScript/wiki/What's-new-in-TypeScript#const-assertions) for literal values.
0 commit comments