Skip to content

Commit 71275e3

Browse files
committed
Explicitly Set Types To Models toJSON Transform Function
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>
1 parent 5500c0b commit 71275e3

4 files changed

Lines changed: 20 additions & 21 deletions

File tree

nextstep-backend/src/models/comments_model.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ const commentSchema: Schema = new mongoose.Schema({
3030
commentSchema.set('toJSON', {
3131
transform: (doc: Document, ret: Record<string, any>) => {
3232
return {
33-
id: ret._id,
34-
postId: ret.postId,
35-
content: ret.content,
36-
owner: ret.owner,
37-
createdAt: ret.createdAt,
38-
updatedAt: ret.updatedAt,
33+
id: ret._id.toString(),
34+
postId: ret.postId.toString(),
35+
content: ret.content as string,
36+
owner: ret.owner.toString(),
37+
createdAt: ret.createdAt ? ret.createdAt.toISOString() : undefined,
38+
updatedAt: ret.updatedAt ? ret.updatedAt.toISOString() : undefined,
3939
};
4040
}
4141
});

nextstep-backend/src/models/company_model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ const companySchema: Schema = new mongoose.Schema({
4646
companySchema.set('toJSON', {
4747
transform: (doc: Document, ret: Record<string, any>) => {
4848
return {
49-
id: ret._id,
50-
company: ret.company,
51-
company_he: ret.company_he,
52-
tags: ret.tags,
49+
id: ret._id.toString(),
50+
company: ret.company as string,
51+
company_he: ret.company_he as string,
52+
tags: ret.tags as string[],
5353
quizzes: ret.quizzes,
5454
}
5555
}

nextstep-backend/src/models/posts_model.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ const postSchema: Schema = new mongoose.Schema({
2525
postSchema.set('toJSON', {
2626
transform: (doc: Document, ret: Record<string, any>): PostData => {
2727
return {
28-
id: ret._id,
28+
id: ret._id.toString(),
2929
title: ret.title,
3030
content: ret.content,
3131
owner: ret.owner._id.toString(),
32-
createdAt: ret.createdAt,
33-
updatedAt: ret.updatedAt
34-
32+
createdAt: ret.createdAt ? ret.createdAt.toISOString() : undefined,
33+
updatedAt: ret.updatedAt ? ret.updatedAt.toISOString() : undefined
3534
};
3635
}
3736
});

nextstep-backend/src/models/user_model.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ const userSchema: Schema = new Schema({
3131
userSchema.set('toJSON', {
3232
transform: (doc, ret): UserData => {
3333
return {
34-
id: ret._id,
35-
username: ret.username,
36-
email: ret.email,
37-
password: ret.password,
38-
imageFilename: ret?.imageFilename,
39-
createdAt: ret.createdAt,
40-
updatedAt: ret.updatedAt
34+
id: ret._id.toString(),
35+
username: ret.username as string,
36+
email: ret.email as string,
37+
password: ret.password as string,
38+
imageFilename: ret?.imageFilename as string | undefined,
39+
createdAt: ret.createdAt ? ret.createdAt.toISOString() : undefined,
40+
updatedAt: ret.updatedAt ? ret.updatedAt.toISOString() : undefined
4141
};
4242
}
4343
});

0 commit comments

Comments
 (0)