Skip to content

Commit 60ffd26

Browse files
committed
update with key decisions
1 parent d6580dc commit 60ffd26

File tree

3 files changed

+137
-20
lines changed

3 files changed

+137
-20
lines changed
3.06 MB
Loading

contributor_docs/pr05_2025_typescript_migration/index.md

Lines changed: 136 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This is the project appendix for the **2025 pr05 Grant: Incremental Typescript M
1616
- [Context](#project-context)
1717
- [Proposed Approach](#proposed-approach)
1818
- [Project Timeline](#project-timeline)
19+
- [Summary of Outcome](#outcome)
20+
- [Key Decisions](#key-decisions)
1921
- [Summary of TS Configurations](#configuration-summary)
2022
- [Migration Tutorial](#migration-tutorial)
2123
- [Index of Migration Examples](#examples-index)
@@ -88,7 +90,9 @@ This is the project appendix for the **2025 pr05 Grant: Incremental Typescript M
8890

8991
### Project Timeline:
9092

91-
<details open>
93+
Click to view details by month
94+
95+
<details>
9296
<summary>July:</summary>
9397

9498
- Set up TS dependencies & configuration on the `root`
@@ -131,7 +135,7 @@ This is the project appendix for the **2025 pr05 Grant: Incremental Typescript M
131135

132136
</details>
133137

134-
<details open>
138+
<details>
135139
<summary>October:</summary>
136140

137141
- Complete migration for `/server/controllers/User`
@@ -149,6 +153,8 @@ This is the project appendix for the **2025 pr05 Grant: Incremental Typescript M
149153

150154
## Outcome:
151155

156+
Click to view details & relevant files per outcome-item
157+
152158
<details>
153159
<summary>All TS-related configuration for dependencies is completed</summary>
154160

@@ -210,42 +216,153 @@ This is the project appendix for the **2025 pr05 Grant: Incremental Typescript M
210216

211217
## Key Decisions:
212218

213-
- Typescript is used for typechecking only, not compiling
219+
<details>
220+
<summary>
221+
Typescript is configured to be used for type checking only, not for transpiling
222+
</summary>
223+
224+
```shell
225+
npm run tsc --noEmit
226+
```
214227

215-
- Define "source-of-truth" types in the `./server/types` folder
228+
- The repo is too large to convert entirely to TS in one go, so we need to flexibly support both TS and JS files
229+
- We have existing legacy runtime build tools (Babel & Webpack) for the existing JS, so this was the easiest way to support both our intended direction and legacy codebase
230+
- Our configured automated type checking prevents PRs from being merged if they fail, so we're working towards a Typescript system without disrupting current build.
231+
- Once the repo has been migrated, we can update to full transpilation with `npm run tsc`
232+
233+
</details>
216234

217-
- Pull any types that are relevant to share with the `./client` into the `./common/types` folder
235+
<details>
236+
<summary>
237+
Prefer `named` exports over `default` exports where possible
238+
</summary>
218239

219-
- Prefer `named` exports over `default` exports
240+
❌ DON'T:
220241

221-
- JSDocs for API endpoints & client components that hit API endpoints
242+
```ts
243+
// calculator.ts
244+
export default function multiply(a: number, b: number): numer {
245+
return a * b;
246+
}
222247

223-
- Will eventually migrate to OpenAPI/Swagger documentation
224-
- Enables devs to hover and see details more easily
248+
// main.ts
249+
import calculate from './calculator'; // the default function is renamed and confusing for next dev to read
225250

226-
- Prefer `interface` over `type`
251+
console.log(calculate(2, 2)); // is this 2*2? 2+2? 2^2? something else?
252+
```
227253

228-
- `/server` types are in `/types` folder
229-
- `/client` types are co-located for now -- pending change
254+
✅ DO:
230255

231-
- Components have their own types so they get messy
256+
```ts
257+
// calculator.ts
258+
export function multiply(a: number, b: number): numer {
259+
return a * b;
260+
}
232261

233-
- If using code-assistance, make sure to write unit tests that cover all happy/unhappy paths to secure against regression. Please see [testing](../testing.md) for more details on general guidelines, or the example code index below.
262+
// main.ts
263+
import { multiply } from './calculator';
264+
265+
console.log(multiply(2, 2));
266+
```
267+
268+
- Default exports allow devs to rename the import anything they want, and inaccurate misnaming will cause confusion
269+
- Renaming default export also makes it more difficult to perform a global search on the function
270+
271+
</details>
272+
273+
<details>
274+
<summary>
275+
Prefer `interface` over `type`
276+
</summary>
277+
278+
- 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.
279+
280+
</details>
281+
282+
<details>
283+
<summary>
284+
If possible, add unit tests prior to the component that you are migrating prior to performing any migration
285+
</summary>
286+
287+
- This protects the app from unintended regression & also helps demonstrate to reviewers that you understand the intent of a component/file you are migrating
288+
- Tests also help document how the component is intended to work.
289+
- Please see [testing](../testing.md) for more details on general guidelines, or the example code index below.
290+
291+
</details>
292+
293+
<details>
294+
<summary>
295+
If a component does not have an existing unit test, keep refactors/logic changes outside of Typescript Migration PRs
296+
</summary
297+
298+
- This helps keep reviews easy for maintainers
299+
- Branch off the migration PR and make a subsequent PR with your proposed refactor.
300+
301+
</details>
302+
303+
<details>
304+
<summary>
305+
Add JSDocs & specify the `Express` `RequestHandler` generics to `/server` controller methods in the following example:
306+
</summary>
307+
308+
```ts
309+
// see server/controllers/user.controller/userPreferences.ts
310+
311+
/**
312+
* - Method: `PUT`
313+
* - Endpoint: `/preferences`
314+
* - Authenticated: `true`
315+
* - Id: `UserController.updatePreferences`
316+
*
317+
* Description:
318+
* - Update user preferences, such as AppTheme
319+
*/
320+
export const updatePreferences: RequestHandler<
321+
{}, // request params type if there are any, extending RouteParam
322+
UpdatePreferencesResponseBody, // response body type
323+
UpdatePreferencesRequestBody // request body type
324+
// query params type if there are any
325+
> = async (req, res) => {
326+
// ... rest of code
327+
};
328+
```
329+
330+
- JSDocs will reduce cognitive load for other developers and enable them to see the controllers details on hover.
331+
- Add the JSDocs to any `client` controllers that use these endpoints.
332+
333+
<img src='./images/hoverJsDocs.gif' alt='hover on a function with jsdocs' width="400px"/>
334+
335+
</details>
336+
337+
<details>
338+
<summary>
339+
Best-effort & evolving-precision principle towards defining types
340+
</summary>
234341

235342
- Make a best effort at being as precise as possible with context clues, but when in doubt, selecting a broader type (eg. `string` instead of an `enum`) is valid and we can update to be stricter as the migration continues.
236343

237-
- Keep refactoring/logic updates outside of migration PRs, unless the relevant file has been secured with a test prior to migration work.
238-
- This helps keep reviews easy for maintainers
239-
- Branch off the migration PR and make a subsequent PR with your proposed refactor.
344+
</details>
345+
346+
<details>
347+
<summary>
348+
`/server` types live in `/server/types`. `/client` types are co-located (currently). Shared types between the `/server` and `/client` live in `/common/types`.
349+
</summary>
350+
351+
- `/server` types are in `/types` folder
352+
- These types should serves the source-of-truth for the rest of the app.
353+
- Pull any types that are relevant to share with the `./client` into the `./common/types` folder
354+
- `/client` types are co-located for now
355+
- Components have their own types so they will get very numerous and messy
356+
- However this is open to proposals for change
357+
358+
</details>
240359

241360
## Configuration Summary:
242361

243362
## Migration Tutorial:
244363

245364
[Video Guide - Migrating the `client/modules/User/pages/AccountView`](youtube.com/watch?v=y84SVy7lAgg&feature=youtu.be)
246365

247-
Text Steps:
248-
249366
## Examples Index:
250367

251368
## Next Step Issues:

contributor_docs/typescript_migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ Below are some guidelines on completing the migration.
99
## Migration Approach:
1010

1111
- [TS Configuration Summary](./pr05_2025_typescript_migration/index.md#configuration-summary)
12-
- [Tutorial](./pr05_2025_typescript_migration/index.md#migration-tutorial)
12+
- [Migration Tutorial](./pr05_2025_typescript_migration/index.md#migration-tutorial)
1313
- [TS & Code style decisions](./pr05_2025_typescript_migration/index.md#key-decisions)

0 commit comments

Comments
 (0)