Commit 71275e3
committed
Explicitly Set Types To Models
Fixes the following error, when running `npm run dev` in the backend:
```
/home/tal/Code/NextStep/nextstep-backend/node_modules/ts-node/src/index.ts:859
return new TSError(diagnosticText, diagnosticCodes, diagnostics);
^
TSError: ⨯ Unable to compile TypeScript:
src/models/user_model.ts:34:13 - error TS2322: Type 'unknown' is not assignable to type 'string'.
34 id: ret._id,
~~
src/types/user_types.ts:13:5
13 id: string;
~~
The expected type comes from property 'id' which is declared here on type 'UserData'
src/models/user_model.ts:35:13 - error TS2322: Type 'unknown' is not assignable to type 'string'.
35 username: ret.username,
~~~~~~~~
src/types/user_types.ts:14:5
14 username: string;
~~~~~~~~
The expected type comes from property 'username' which is declared here on type 'UserData'
src/models/user_model.ts:36:13 - error TS2322: Type 'unknown' is not assignable to type 'string'.
36 email: ret.email,
~~~~~
src/types/user_types.ts:15:5
15 email: string;
~~~~~
The expected type comes from property 'email' which is declared here on type 'UserData'
src/models/user_model.ts:37:13 - error TS2322: Type 'unknown' is not assignable to type 'string'.
37 password: ret.password,
~~~~~~~~
src/types/user_types.ts:16:5
16 password: string;
~~~~~~~~
The expected type comes from property 'password' which is declared here on type 'UserData'
src/models/user_model.ts:38:13 - error TS2322: Type 'unknown' is not assignable to type 'string | undefined'.
38 imageFilename: ret?.imageFilename,
~~~~~~~~~~~~~
src/types/user_types.ts:17:5
17 imageFilename?: string;
~~~~~~~~~~~~~
The expected type comes from property 'imageFilename' which is declared here on type 'UserData'
src/models/user_model.ts:39:13 - error TS2322: Type 'unknown' is not assignable to type 'string | undefined'.
39 createdAt: ret.createdAt,
~~~~~~~~~
src/types/user_types.ts:18:5
18 createdAt?: string,
~~~~~~~~~
The expected type comes from property 'createdAt' which is declared here on type 'UserData'
src/models/user_model.ts:40:13 - error TS2322: Type 'unknown' is not assignable to type 'string | undefined'.
40 updatedAt: ret.updatedAt
~~~~~~~~~
src/types/user_types.ts:19:5
19 updatedAt?: string,
~~~~~~~~~
The expected type comes from property 'updatedAt' which is declared here on type 'UserData'
at createTSError (/home/tal/Code/NextStep/nextstep-backend/node_modules/ts-node/src/index.ts:859:12)
at reportTSError (/home/tal/Code/NextStep/nextstep-backend/node_modules/ts-node/src/index.ts:863:19)
at getOutput (/home/tal/Code/NextStep/nextstep-backend/node_modules/ts-node/src/index.ts:1077:36)
at Object.compile (/home/tal/Code/NextStep/nextstep-backend/node_modules/ts-node/src/index.ts:1433:41)
at Module.m._compile (/home/tal/Code/NextStep/nextstep-backend/node_modules/ts-node/src/index.ts:1617:30)
at node:internal/modules/cjs/loader:1895:10
at Object.require.extensions.<computed> [as .ts] (/home/tal/Code/NextStep/nextstep-backend/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1465:32)
at Function._load (node:internal/modules/cjs/loader:1282:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14) {
diagnosticCodes: [
2322, 2322,
2322, 2322,
2322, 2322,
2322
]
}
[nodemon] app crashed - waiting for file changes before starting...
```
**Problem**
In `user_model.ts`, the `transform` function is typed
to return `UserData`, which expects all fields to be
of type `string` (or `string | undefined` for some).
However, the `ret` object passed to the transform
function is not strongly typed—it is inferred as
`any` or `unknown` by TypeScript, especially when
using strict settings.
This fixes all the models` `transform` function.
Signed-off-by: Tal Jacob <taljacob2@gmail.com>toJSON Transform Function1 parent 5500c0b commit 71275e3
4 files changed
Lines changed: 20 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | | - | |
34 | | - | |
| 32 | + | |
| 33 | + | |
35 | 34 | | |
36 | 35 | | |
37 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
0 commit comments