Code conventions
Keep code as simple as possible so it's easy to read later. Split a function only when it gets very long or its logic is reused. Don't extract constants or types unless they're reused.
TypeScript
- Use strict types everywhere. Reuse existing types where you can.
- Write functions as
const name = () => {}, notfunction name() {}. - End every import path with
.ts, not.js.
Types
- Inline a type that's only used once in a file, including props, context values, parameters and return types.
- Don't create
FooProps,FooStateorFooContextTypealiases unless that exact type is used more than once in the same file. - Put a type used across several files in its own type file, or an existing one on the same theme.
- Readability comes first. If you extract a local type to make code easier to read, put function types at the bottom of the file and other types near where they're first used.
const ExampleContext = createContext<{
value: string;
setValue: (value: string) => void;
} | null>(null);
export const ExampleProvider = ({ children }: { children: React.ReactNode }) => {Errors
Use the project's tryCatch helper instead of try/catch:
const result = await tryCatch(addTwoNumbers(1, 2));
if (result.error) {
console.log("ERROR");
return;
}
console.log(result.data);When you're awaiting many calls together, a plain try/catch is fine if it's simpler.
Comments
Don't leave comments. Code should explain itself. The exception is working around a quirk in someone else's API.
The app
Use Expo Router's Link component for links between screens.
Dependencies
Install the latest version of a package with bun install; don't pick a version number by hand.