ashwanikumarjha
Mon Oct 23 2023
In TypeScript, type annotations are removed when transpiling to JavaScript. This make us believe that type information is lost in JavaScript runtime. However, TypeScript offers a compiler option called emitDecoratorMetadata
that, when enabled, emits metadata about the types used in our code. This metadata is accessible at runtime using reflect-metadata
library.
Libraries like class-transformer
and class-validator
leverage this metadata to transform plain JavaScript objects into instances of specific classes and validate them against certain rules. Even though TypeScript types don't exist at runtime, the information about those types does, this provide us a more structured and safe way of working with data in JavaScript.