Quickstart
Overview
Typeconf is a library that allows you to write configs in Typescript and produce JSONs while keeping the type validation. With Typeconf you create a configuration directory or package and manage all your configs there. Then for your configs we generate types in your project language and you can read them just list a regular object in code.
Currently, Typeconf only supports TypeScript, but we plan to add support for other languages in the near future.
Typespec schema
In Typeconf, configurations are defined using schemas and values separately. The schema is written using a TypeSpec-based language, which is a TypeScript-like syntax. Learn more about TypeSpec here. Here’s an example of such schema:
Typescript configuration files
To set the values of the configuration file you can create a TypeScript file with the name <config-name>.config.ts
inside your configuration
directory and this file will be compiled to JSON after build. You can create multiple files, this is useful for separating environments or
different parts of the application.
Continuing our example, here are the values for the model defined above:
Generated JSON files
After changing the config.ts file you can run npx @typeconf/typeconf build configs
to produce JSON file which you can read in your app, either with
Typeconf SDK with type validation or just as a plain JSON.
Here’s the JSON from our example:
Project structure
In project
The most straighforward way to use Typeconf is to create a separate directory in your project and put configs there:
Standalone
If you want to share configuration between multiple projects or inherit types you can create a separate package with this helper CLI:
This will generate all the necessary boilerplate and you will be able to update configs with npm run build
.
CLI
If you’re not using a separate package you need to use Typeconf CLI to update the configs:
Using the config in your code
To use your configuration in your code, you can install the configuration package you just created as a dependency. Paired with the package you need to install Typeconf SDK:
Once everything is installed, you can read the configuration from any JSON file like this:
FAQ
How to update changes locally if I use a separate package for configs?
Use npx link@latest <path/to/package>
to add a symlink to local version of the package.
Where can I see more examples
Check out the examples in the repository. Or check out playground, you can try out the examples without installing it locally.
Was this page helpful?