Skip to content

Commit 8e0a35a

Browse files
committed
add example code
1 parent feb136d commit 8e0a35a

File tree

1 file changed

+124
-7
lines changed
  • contributor_docs/pr05_2025_typescript_migration

1 file changed

+124
-7
lines changed

contributor_docs/pr05_2025_typescript_migration/index.md

Lines changed: 124 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ This is the project appendix for the **2025 pr05 Grant: Incremental Typescript M
1010

1111
## Table of Contents
1212

13-
<details open>
14-
1513
- [Project Outline](#project-outline)
1614
- [Context](#project-context)
1715
- [Proposed Approach](#proposed-approach)
@@ -23,7 +21,6 @@ This is the project appendix for the **2025 pr05 Grant: Incremental Typescript M
2321
- [Index of Migration Examples](#examples-index)
2422
- [Next Step Issues](#next-step-issues)
2523
- [Additional Reading](#additional-reading)
26-
</details>
2724

2825
## Project Outline:
2926

@@ -277,6 +274,27 @@ console.log(multiply(2, 2));
277274
Prefer `interface` over `type`
278275
</summary>
279276

277+
```tsx
278+
// ✅ interface
279+
interface ButtonProps {
280+
label: string
281+
disabled?: boolean
282+
onClick: () => void
283+
}
284+
const Button = ({ label, disabled, onClick }: ButtonProps) => (...)
285+
286+
// ❌ type
287+
type ButtonProps = {
288+
label: string
289+
disabled?: boolean
290+
onClick: () => void
291+
}
292+
293+
// ✅ keep type for primitives
294+
type UserId = string;
295+
```
296+
297+
- Clearer semantic meaning when describing structured objects vs. primitives
280298
- This is not strictly enforced in linting rules, but aims to align with [Google Typescript styling](https://google.github.io/styleguide/tsguide.html#prefer-interfaces). Please see attached link for details.
281299

282300
</details>
@@ -375,15 +393,114 @@ export const updatePreferences: RequestHandler<
375393

376394
<summary>
377395
<details>
378-
Text Guide
396+
<summary>
397+
Text Guide
398+
</summary>
399+
TO DO
379400
</details>
380-
381-
- TODO
382-
383401
</summary>
384402

385403
## Examples Index:
386404

405+
### Client Files:
406+
407+
<details>
408+
<summary>React component</summary>
409+
410+
- [IconButton](https://github.com/processing/p5.js-web-editor/blob/develop/client/common/IconButton.tsx)
411+
- [IconButton test](https://github.com/processing/p5.js-web-editor/blob/develop/client/common/IconButton.test.tsx)
412+
413+
</details>
414+
415+
<details>
416+
<summary>Normal TS file</summary>
417+
418+
- [formatDate](https://github.com/processing/p5.js-web-editor/blob/develop/client/utils/formatDate.ts)
419+
- [formateDate test](https://github.com/processing/p5.js-web-editor/blob/develop/client/utils/formatDate.test.ts)
420+
421+
</details>
422+
423+
<details>
424+
<summary>Hook</summary>
425+
426+
- [usePrevious](https://github.com/processing/p5.js-web-editor/blob/develop/client/common/usePrevious.ts)
427+
- [usePrevious test](https://github.com/processing/p5.js-web-editor/blob/develop/client/common/usePrevious.test.ts)
428+
429+
</details>
430+
431+
<details>
432+
<summary>Redux</summary>
433+
434+
- To be updated pending open PR
435+
436+
</details>
437+
438+
<details>
439+
<summary>Custom type declaration file</summary>
440+
441+
- [custom.d.ts](https://github.com/processing/p5.js-web-editor/blob/develop/client/custom.d.ts)
442+
- Use this to extend any client namespaced types (eg. Window) or file extensions (.svg, .mp3)
443+
444+
</details>
445+
446+
### Server Files:
447+
448+
<details>
449+
<summary>Types are extracted into /server/types in a barrel structure</summary>
450+
451+
- [types/userPreferences](https://github.com/processing/p5.js-web-editor/blob/develop/server/types/userPreferences.ts)
452+
- [types/index](https://github.com/processing/p5.js-web-editor/blob/develop/server/types/index.ts)
453+
- All server types are exported here
454+
455+
</details>
456+
457+
<details>
458+
<summary>Routes</summary>
459+
460+
- [routes/user.routes.ts](https://github.com/processing/p5.js-web-editor/blob/develop/server/routes/user.routes.ts)
461+
- Format the routes by 'subdomain' if the file is particularly large
462+
- Add the method & path in code comments for easier global search
463+
464+
</details>
465+
466+
<details>
467+
<summary>Model</summary>
468+
469+
- [models/user](https://github.com/processing/p5.js-web-editor/blob/develop/server/models/user.ts)
470+
- [models/user test](https://github.com/processing/p5.js-web-editor/blob/develop/server/models/__test__/user.ts)
471+
472+
</details>
473+
474+
<details>
475+
<summary>Controller</summary>
476+
477+
- [controllers/user.controller > index file](https://github.com/processing/p5.js-web-editor/blob/develop/server/controllers/user.controller/index.ts)
478+
- Note that controllers should be organised in barrel format with an index file.
479+
- [controllers/user.controller > updateSettings method](https://github.com/processing/p5.js-web-editor/blob/develop/server/controllers/user.controller/userPreferences.ts)
480+
- [controllers/user.controller > updateSettings method test](https://github.com/processing/p5.js-web-editor/blob/develop/server/controllers/user.controller/__tests__/userPreferences.test.ts)
481+
482+
</details>
483+
484+
<details>
485+
<summary>Custom type declaration for a namespace for a dependency</summary>
486+
487+
- [types/express](https://github.com/processing/p5.js-web-editor/blob/develop/server/types/express/index.d.ts)
488+
- Extend the `express` namespace `Request.User` with a custom user type.
489+
- [types/jest-express](https://github.com/processing/p5.js-web-editor/blob/develop/server/types/jest-express/index.d.ts)
490+
- Parallel change on `jest-express` to extend `Request.User` in the test environment
491+
492+
</details>
493+
494+
### Common Files:
495+
496+
<details>
497+
<summary>Types shared across the server to client are exported in /common/types</summary>
498+
499+
- [types/index](https://github.com/processing/p5.js-web-editor/blob/develop/common/types/index.ts)
500+
- Note that for some APIs (eg. user) we don't want to export **all** types, we want to specifically name the types to share with the client (eg. not `UserDocument`)
501+
502+
</details>
503+
387504
## Next Step Issues:
388505

389506
- Migration for the rest of the repo

0 commit comments

Comments
 (0)