Home > Blockchain >  Does type checking occur after compiling typescript file?
Does type checking occur after compiling typescript file?

Time:06-21

I'm making a simple web server using nest.js!

This is my code in a controller file.

  @Delete('users/:id/courses/:course_id')
  deleteUserCourses(
    @Param() { id, course_id }: { id: number; course_id: string },
  ) {
    return this.usersService.deleteUserCourses(id, course_id);
  }

When I sent a delete request message toward "/users/123/courses/math",

I expected a type error on variable id, because id and course_id have string type.

However, I didn't get any error or warning.

Is that because the compiled typescript file does not check variable types in runtime?

Or there are any other reasons that type checking does not occur?

Thanks in advance

CodePudding user response:

Typechecking is done at compile time.

The runtime will run plain JS, that's what all the transpilers do: transform your TS code files to JS files.

CodePudding user response:

You're right, TS doesn't check types in runtime.

CodePudding user response:

For adding validation in runtime you should use dto objects and validation technic that nestjs is providing via class-validator package. Read more about it here

  • Related