@@ -25,7 +25,7 @@ There are several reasons for this:
25
25
26
26
🚫 Type declarations
27
27
28
- ``` ts
28
+ ``` typescript
29
29
const name: string = ' METAMASK' ; // Type 'string'
30
30
31
31
const BUILT_IN_NETWORKS = new Map <string , ` 0x${string } ` >([
@@ -36,7 +36,7 @@ const BUILT_IN_NETWORKS = new Map<string, `0x${string}`>([
36
36
37
37
✅ Type inferences
38
38
39
- ``` ts
39
+ ``` typescript
40
40
const name = ' METAMASK' ; // Type 'METAMASK'
41
41
42
42
const BUILT_IN_NETWORKS = {
@@ -47,7 +47,7 @@ const BUILT_IN_NETWORKS = {
47
47
48
48
#### Example: To annotate or not to annotate
49
49
50
- ``` ts
50
+ ``` typescript
51
51
type TransactionMeta = TransactionBase &
52
52
(
53
53
| {
@@ -73,7 +73,7 @@ this.messagingSystem.publish(
73
73
74
74
🚫 Add type annotation
75
75
76
- ``` ts
76
+ ``` typescript
77
77
// Type 'TransactionMeta'
78
78
const updatedTransactionMeta: TransactionMeta = {
79
79
... transactionMeta ,
@@ -83,7 +83,7 @@ const updatedTransactionMeta: TransactionMeta = {
83
83
84
84
✅ Add ` as const ` and leave to inference
85
85
86
- ``` ts
86
+ ``` typescript
87
87
// Type narrower than 'TransactionMeta': { status: TransactionStatus.rejected; ... }
88
88
// (doesn't include 'error' property)
89
89
const updatedTransactionMeta = {
@@ -104,15 +104,15 @@ There is a clear exception to the above: if an explicit type annotation or asser
104
104
105
105
🚫
106
106
107
- ``` ts
107
+ ``` typescript
108
108
const chainId: string = this .messagingSystem (
109
109
' NetworkController:getProviderConfig' ,
110
110
).chainId ; // Type 'string'
111
111
```
112
112
113
113
✅
114
114
115
- ``` ts
115
+ ``` typescript
116
116
const chainId = this .messagingSystem (
117
117
' NetworkController:getProviderConfig' ,
118
118
).chainId ; // Type '`0x${string}`'
@@ -124,14 +124,14 @@ This is one case where type inference is unable to reach a useful conclusion wit
124
124
125
125
🚫
126
126
127
- ``` ts
127
+ ``` typescript
128
128
const tokens = []; // Type 'any[]'
129
129
const tokensMap = new Map (); // Type 'Map<any, any>'
130
130
```
131
131
132
132
✅
133
133
134
- ``` ts
134
+ ``` typescript
135
135
const tokens: string [] = []; // Type 'string[]'
136
136
const tokensMap = new Map <string , Token >(); // Type 'Map<string, Token>'
137
137
```
@@ -140,7 +140,7 @@ const tokensMap = new Map<string, Token>(); // Type 'Map<string, Token>'
140
140
141
141
<!-- TODO: Add explanation and examples -->
142
142
143
- ``` ts
143
+ ``` typescript
144
144
function isSomeInterface(x : unknown ): x is SomeInterface {
145
145
return (
146
146
' name' in x &&
@@ -211,7 +211,7 @@ Otherwise, only mocking the properties needed in the test improves readability b
211
211
212
212
< ! -- TODO : Add examples -- >
213
213
214
- ` ` ` ts
214
+ ` ` ` typescript
215
215
const handler:
216
216
| ((payload_0: ComposableControllerState, payload_1: Patch[]) => void)
217
217
| ((payload_0: any, payload_1: Patch[]) => void); // Type of 'payload_0': 'any'
@@ -236,7 +236,7 @@ Some generic types use `any` as a default generic argument. This can silently in
236
236
237
237
🚫
238
238
239
- ` ` ` ts
239
+ ` ` ` typescript
240
240
const NETWORKS = new Map({
241
241
mainnet: '0x1',
242
242
goerli: '0x5',
@@ -251,7 +251,7 @@ mockGetNetworkConfigurationByNetworkClientId.mockImplementation(
251
251
252
252
✅
253
253
254
- ` ` ` ts
254
+ ` ` ` typescript
255
255
const NETWORKS = new Map<string, ` 0x$ {string }` >({
256
256
mainnet: '0x1',
257
257
goerli: '0x5',
@@ -276,7 +276,7 @@ In most type errors involving property access or runtime property assignment, `a
276
276
277
277
🚫
278
278
279
- ` ` ` ts
279
+ ` ` ` typescript
280
280
for (const key of getKnownPropertyNames(this.internalConfig)) {
281
281
(this as any)[key] = this.internalConfig[key];
282
282
}
@@ -288,7 +288,7 @@ delete addressBook[chainId as any];
288
288
289
289
✅
290
290
291
- ` ` ` ts
291
+ ` ` ` typescript
292
292
for (const key of getKnownPropertyNames(this.config)) {
293
293
(this as unknown as typeof this.config)[key] = this.config[key];
294
294
}
@@ -300,20 +300,20 @@ However, when assigning to a generic type, using `as any` is the only solution.
300
300
301
301
🚫
302
302
303
- ` ` ` ts
303
+ ` ` ` typescript
304
304
(state as RateLimitState<RateLimitedApis>).requests[api][origin] = previous + 1;
305
305
// is generic and can only be indexed for reading.ts(2862)
306
306
` ` `
307
307
308
308
✅
309
309
310
- ` ` ` ts
310
+ ` ` ` typescript
311
311
(state as any).requests[api][origin] = previous + 1;
312
312
` ` `
313
313
314
314
Even in this case , however , ` any ` usage might be avoidable by using ` Object.assign ` or spread operator syntax instead of assignment .
315
315
316
- ` ` ` ts
316
+ ` ` ` typescript
317
317
Object.assign(state, {
318
318
requests: {
319
319
...state.requests,
@@ -334,7 +334,7 @@ Object.assign(state, {
334
334
335
335
✅
336
336
337
- ` ` ` ts
337
+ ` ` ` typescript
338
338
class BaseController<
339
339
...,
340
340
messenger extends RestrictedControllerMessenger<N, any, any, string, string>
@@ -362,7 +362,7 @@ Although TypeScript is capable of inferring return types, adding them explicitly
362
362
363
363
🚫
364
364
365
- ` ` ` ts
365
+ ` ` ` typescript
366
366
async function removeAccount(address: Hex) {
367
367
const keyring = await this.getKeyringForAccount(address);
368
368
@@ -379,7 +379,7 @@ async function removeAccount(address: Hex) {
379
379
380
380
✅
381
381
382
- ` ` ` ts
382
+ ` ` ` typescript
383
383
async function removeAccount(address: Hex): Promise<KeyringControllerState> {
384
384
const keyring = await this.getKeyringForAccount(address);
385
385
0 commit comments