Skip to content

Commit

Permalink
update docusaurus
Browse files Browse the repository at this point in the history
  • Loading branch information
mkosir committed Mar 28, 2024
1 parent 79d25c4 commit cd0661d
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions website/src/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,30 @@ const [userRole, setUserRole] = useState<UserRole>('admin'); // Type 'UserRole'

### Data Immutability

Majority of the data should be immutable - use `Readonly`, `ReadonlyArray`, always return new array, object etc.
To keep cognitive load for future developers low, try to keep data objects small.
Majority of the data should be immutable with use of `Readonly`, `ReadonlyArray`.

When performing data processing always return new array, object etc. To keep cognitive load for future developers low, try to keep data objects small.
As an exception mutations should be used sparingly in cases where truly necessary: complex objects, performance reasoning etc.

```ts
// ❌ Avoid data mutations
const removeFirstUser = (users: Array<User>) => {
if (users.length === 0) {
return users;
}
return users.splice(1);
};

// ✅ Use readonly type
const removeFirstUser = (users: ReadonlyArray<User>) => {
if (users.length === 0) {
return users;
}
return users.slice(1);
// Using arr.splice(1) errors - Function 'splice' does not exist on 'users'
};
```

### Return Types

Including return type annotations is highly encouraged, although not required ([eslint rule](https://typescript-eslint.io/rules/explicit-function-return-type/)).
Expand Down

0 comments on commit cd0661d

Please sign in to comment.