Skip to content

Commit a3425c1

Browse files
committed
Add arrow function usage guideline to docs
1 parent 13e4732 commit a3425c1

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

  • apps/web/src/app/(docs)/docs/conventions

apps/web/src/app/(docs)/docs/conventions/page.mdx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,47 @@ const bar = () => {
6464
};
6565
```
6666

67+
## Arrow functions
68+
69+
Use arrow functions instead of the `function` keyword.
70+
71+
```ts
72+
// Prefer
73+
export const createUser = (data: UserData): User => {
74+
// implementation
75+
};
76+
77+
// Avoid
78+
export function createUser(data: UserData): User {
79+
// implementation
80+
}
81+
```
82+
83+
Why arrow functions?
84+
85+
- **No hoisting** - Combined with `const`, arrow functions aren't hoisted, which enforces top-down code organization
86+
- **Consistency** - One way to define functions means less cognitive overhead
87+
- **Currying** - Arrow functions make currying natural for [dependency injection](/docs/dependency-injection)
88+
89+
**Exception: function overloads.** TypeScript requires the `function` keyword for overloaded signatures:
90+
91+
```ts
92+
export function mapArray<T, U>(
93+
array: NonEmptyReadonlyArray<T>,
94+
mapper: (item: T) => U,
95+
): NonEmptyReadonlyArray<U>;
96+
export function mapArray<T, U>(
97+
array: ReadonlyArray<T>,
98+
mapper: (item: T) => U,
99+
): ReadonlyArray<U>;
100+
export function mapArray<T, U>(
101+
array: ReadonlyArray<T>,
102+
mapper: (item: T) => U,
103+
): ReadonlyArray<U> {
104+
return array.map(mapper) as ReadonlyArray<U>;
105+
}
106+
```
107+
67108
## Immutability
68109

69110
Mutable state is tricky because it increases the risk of unintended side effects, makes code harder to predict, and complicates debugging—especially in complex applications where data might be shared or modified unexpectedly. Favor immutable values using readonly types to reduce these risks and improve clarity.

0 commit comments

Comments
 (0)