Typescript annotations

I am writing a project on MarionetteJS using TS, and I want to use annotations to register routes. For instance:

@Controller class SomeController { @RouteMapping("some route") public SomeMethod() { //code } } 

Is it possible to call method annotations when creating an instance of a class?

+5
source share
3 answers

No no.

Check out this article Designing Decorators and Metadata in TypeScript . It shows how decorators are converted to pure javascript, and therefore, when called.

+4
source

I use the ORM framework in TypeScript (TypeORM) and can set something using annotations (e.g. @entity (database: '')).

Can I get the parameter value from the annotation from a .env file or class with constants?

I have tried:

 @entity(database: process.env.DATA_BASE_NAME) class UserEntity {...} 

Without success = / Because at that moment my .env was not created yet = /

But it works fine

 @entity(database: 'Users') class UserEntity { dotenv.config(); @(column: process.env.TABLE_USER_COL_ID_NAME, ...) private id: number; } 

Any advice to solve this?

0
source

I am using typcript-rest

Allows you to configure services with annotation and, if necessary, generates a Swagger file.

0
source

Source: https://habr.com/ru/post/1245060/


All Articles