#Does Nestjs use some sort of its own compiler?

10 messages · Page 1 of 1 (latest)

fringe token
#

I'm just starting out with learning nestjs and playing around with examples from the docs

This snippet of code works, cats module defines providers and controllers etc.

@Controller('cats')
export class CatsController {
  constructor(private readonly catsService: CatsService) {}
// etc...

BUT
If I remove the type annotation

@Controller('cats')
export class CatsController {
  constructor(private readonly catsService) {}
// etc...

The app breaks - since I'm used to type annotations not affecting runtime at all I have to wonder why does it matter. My only guess would be that Nestjs implements its own compiler? or I've heard typescript allows to write plugins to the compiler - something like that?

#

this is just a question out of curiosity how nestjs works

rocky forge
#

@fringe token - Do you know how TypeScript works with JavaScript?

fringe token
# rocky forge <@241333871747137548> - Do you know how TypeScript works with JavaScript?

that's a very broad question - I mean I'm aware that ts compiles to js. It does some type-checking, strips away the types, converts some ts specific features(like decorators or property modifiers in constructors) to js compatible code, maybe a little bit of transpilation - something like that.

Never have I encountered a scenario where a type annotation would affect the resulting code - except for a compilation error due to type-checking.
The closest thing I saw was a library called Typia which used ts compiler plugins to use type annotations to construct runtime validators.

#

My guess was that also maybe since class becomes unused the build tools strip away the import and that breaks somehow but this snippet also crashes

@Controller('cats')
export class CatsController {
  private readonly catsService: CatsService;
  constructor(catsService) {
    this.catsService = catsService;
  }
// etc...
rocky forge
#

Ok. Just needed to understand your level of knowledge. The piece you are missing is how Nest uses reflect-metadata to use TypeScript for its initialization/ bootstrapping process. I'm not 100% in the know about how the internals works, as it is Nest's black-box feature, but Nest needs the types to basically know what to wire up. That's my understanding. @hushed heron Jay could probably explain better and correct me if I am wrong. 🙂

fringe token
#

Yeah, I saw that you use reflect in decorators to set-up controllers and stuff - didn't know it gets access to type annotation data tho, I thought it was limited to prototype, property name - that kind of thing

#

Ok, I see - "design:paramtypes" metadata in Reflect has info on type annotation it seems, thanks

ripe heraldBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution if you are having the same issue, and then re-open the post if you are still having trouble, providing as much extra information as possible.