In this example repository, I am going to show you how to enforce TypeScript types in the runtime environment in your projects
I also published article on:
- Medium - How to enforce TypeScript types in the runtime environment
- My Gists - How to enforce TypeScript types in the runtime environment
- ✅ Generate validators for batch of types definition files via script code
- ✅ Check whether file has changed by using
folder-hash
. Compare checksum hash value to know file changed
- Clone this repository into your local machine
- Install node modules by using
yarn install
ornpm install
- Run script to generate validators
yarn generate
This script helps us to generate multiple validator files, with matched types file **/*.types.ts
under src
folder.
If you are a Windows users, you should open the WSL (Windows Subsystem of Linux) terminal to run the script above. Because this does not work well in Windows platform.
Related to this issue, I leave here the issue reference from the typescript-json-validator
repository Not working in windows #36
Build our TypeScript project to JavaScript using tsc
CLI command supported by typescript
.
Run TypeScript file src/main.ts
directly, without compiling to JavaScript
Example run result
After run build
script, you can get the project build in dist
folder.
So now, just use node dist/main.js
to run the example.
It still works as expected like above.