Skip to content

phatnguyenuit/enforce-typescript-types-runtime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to enforce TypeScript types in the runtime environment

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:

Table of contents

Feature

  • ✅ 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

Usage

  • Clone this repository into your local machine
  • Install node modules by using yarn install or npm install
  • Run script to generate validators yarn generate

Scripts

1. yarn generate

This script helps us to generate multiple validator files, with matched types file **/*.types.ts under src folder.

Notes

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

2. yarn build

Build our TypeScript project to JavaScript using tsc CLI command supported by typescript.

3. npx ts-node src/main.ts

Run TypeScript file src/main.ts directly, without compiling to JavaScript

Example run result

Example result with TypeScript file

4. node dist/main.js

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.

Example result with JavaScript file

References

About

How to enforce TypeScript types in the runtime environment

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published