Skip to content

Commit 438c337

Browse files
committed
Add note to typescript guide
1 parent 592df49 commit 438c337

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

docs/typescript.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,14 +974,19 @@ The concept of the interface as discussed in this section is not to be confused
974974
975975
TypeScript offers several tools for crafting clear data definitions, with enumerations and unions standing as popular choices.
976976
977-
#### Consider using enums over union types for situations with a fixed set of known values.
977+
> [!NOTE]
978+
> `enum` syntax is being phased out of TypeScript. v5.8 introduces a new flag `erasableSyntaxOnly`, which will mark enums as errors (along with namespaces and class parameter properties).
979+
>
980+
> The recommended alternative is to use objects defined with the `as const` keyword (see https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums).
981+
982+
#### Consider using `as const` objects over union types when enumerating a fixed set of known values.
978983
979984
Inevitably you will want to refer to the values of a union type somewhere (perhaps as the argument to a function). You can of course just use a literal which represents a member of that union — but if you have an enum, then all of the values are special, and any time you use a value then anyone can see where that value comes from.
980985
981986
🚫
982987
983988
```typescript
984-
type UserRole = 'admin' | 'editor' | 'subscriber';
989+
type AccountTypes = 'admin' | 'user' | 'guest';
985990
```
986991
987992
@@ -992,6 +997,9 @@ enum AccountType {
992997
User = 'user',
993998
Guest = 'guest',
994999
}
1000+
1001+
// 'admin' | 'user' | 'guest'
1002+
type AccountTypes = `${AccountType};
9951003
```
9961004

9971005
#### Don't use numeric enums
@@ -1025,9 +1033,28 @@ const directions = Object.values(Direction); // ["Up", "Down", "Left", "Right"]
10251033

10261034
## Functions
10271035

1028-
#### For functions and methods, provide explicit return types
1036+
#### Avoid return type annotations for functions and methods. Instead, prefer `satisfies` and the JSDoc `@returns` tag.
1037+
1038+
Explicit return type annotations on functions overwrite narrower types inferred by the compiler
1039+
1040+
can artificially widen the function's type signature, resulting in a loss of type information. Relying on type inference guarantees .
1041+
1042+
> Enforcing a wider type defeats the purpose of adding an explicit type declaration, as it loses type information instead of adding it.
1043+
>
1044+
> -- https://github.com/MetaMask/contributor-docs/blob/main/docs/typescript.md#avoid-unintentionally-widening-an-inferred-type-with-a-type-annotation
1045+
1046+
There are two ways that explicit function return types can be useful. Fortunately there are
10291047

1030-
Although TypeScript is capable of inferring return types, adding them explicitly makes it much easier for the reader to see the API from the code alone and prevents unexpected changes to the API from emerging.
1048+
1049+
1050+
1.
1051+
1052+
Unfortunately, this comes at
1053+
1054+
artificially widen the return type
1055+
1056+
1057+
makes it much easier for the reader to see the API from the code alone and prevents unexpected changes to the API from emerging.
10311058

10321059
**Example <a id="example-a88b18ef-b066-4aa7-8106-bc244298f9e6"></a> ([🔗 permalink](#example-a88b18ef-b066-4aa7-8106-bc244298f9e6)):**
10331060

@@ -1051,7 +1078,7 @@ async function removeAccount(address: Hex) {
10511078
10521079
10531080
```typescript
1054-
async function removeAccount(address: Hex): Promise<KeyringControllerState> {
1081+
async function removeAccount(address: Hex) {
10551082
const keyring = await this.getKeyringForAccount(address);
10561083

10571084
if (!keyring.removeAccount) {
@@ -1061,7 +1088,7 @@ async function removeAccount(address: Hex): Promise<KeyringControllerState> {
10611088
this.emit('removedAccount', address);
10621089

10631090
await this.persistAllKeyrings();
1064-
return this.fullUpdate();
1091+
return this.fullUpdate() satisfies KeyringControllerState;
10651092
}
10661093
```
10671094

0 commit comments

Comments
 (0)