1

recently i've been asked to implement config files for my system, config for each environment. When i wanted to use the config i noticed that i dont have it typed, at least not in easy way.. So i created an index file which import and export the config adding an interface to it.

I wonder if i can add a type to my config (somehow) which will force the developers to stick to it and also provide us a type at compilation time.

Its like to have a config.ts file instead of config.json (maybe that what i should do?)

Thanks!

1 Answers1

0

Yes you can - use json schema for this purpose.

To ensure your develpers have not messed up the data - use one of many available validators (i find this one to work pretty nice) to verify json object you read from config file.

Some IDE (vscode for example) support json schemas and can validate it on the fly.

You can have your config stored in *.ts file as exported const variable. Then after requiring it verify it with validator. Unfortunately this approach will not give you errors at compilation type.

Community
  • 1
  • 1
Amid
  • 21,508
  • 5
  • 57
  • 54