Skip to content

Commit 77a0c1c

Browse files
committed
chore: update conventions to explicitly ban TypeScript enum
Convention #3 already said to use `as const` arrays, but didn't explicitly call out TypeScript `enum` as bad. Enums cause type cast issues when crossing WASM/serde boundaries. Added enum to the "Bad" examples and added WASM/serde reasoning to "Why". BTC-3226
1 parent 7d48d0a commit 77a0c1c

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

CONVENTIONS.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,17 @@ const total = parseInt(amount) + parseInt(fee); // ❌ Loses precision
104104
- IDE autocomplete
105105
- Exhaustiveness checking in switch statements
106106
- Less repetitive than `enum` (no `Key = "Key"` duplication)
107+
- TypeScript `enum` values cause type cast issues when crossing WASM/serde boundaries. The `as const` pattern avoids this entirely.
107108

108109
**Good:**
109110

110111
```typescript
111112
export const TransactionType = ["Send", "StakingActivate", "StakingDeactivate"] as const;
112113
export type TransactionType = (typeof TransactionType)[number];
113114

115+
export const TonStakingType = ["TonWhales", "SingleNominator", "MultiNominator"] as const;
116+
export type TonStakingType = (typeof TonStakingType)[number];
117+
114118
function handleTx(type: TransactionType) {
115119
switch (type) {
116120
case "Send":
@@ -125,11 +129,16 @@ function handleTx(type: TransactionType) {
125129
**Bad:**
126130

127131
```typescript
132+
// ❌ TypeScript enum — causes type cast issues across WASM boundaries
133+
enum TransactionType {
134+
Send = "Send",
135+
StakingActivate = "StakingActivate",
136+
}
137+
128138
// ❌ No type safety, typos not caught
129139
function handleTx(type: string) {
130140
if (type === "send") {
131141
// Oops, wrong case
132-
// ...
133142
}
134143
}
135144

0 commit comments

Comments
 (0)