https://github.com/phatnguyenuit/enforce-typescript-types-runtime
How to enforce TypeScript types in the runtime environment
https://github.com/phatnguyenuit/enforce-typescript-types-runtime
enforce runtime types typescript
Last synced: 9 months ago
JSON representation
How to enforce TypeScript types in the runtime environment
- Host: GitHub
- URL: https://github.com/phatnguyenuit/enforce-typescript-types-runtime
- Owner: phatnguyenuit
- License: mit
- Created: 2021-11-25T12:14:17.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-02T03:13:11.000Z (over 4 years ago)
- Last Synced: 2025-02-27T21:44:29.176Z (over 1 year ago)
- Topics: enforce, runtime, types, typescript
- Language: TypeScript
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
- [Medium - How to enforce TypeScript types in the runtime environment](https://medium.com/weekly-webtips/how-to-enforce-typescript-types-in-the-runtime-environment-3564ae33a792?source=friends_link&sk=c7d164b79be7829ac6f3a92fcfe7ada9)
- [My Gists - How to enforce TypeScript types in the runtime environment](https://gist.github.com/phatnguyenuit/8c5d2acb9a458f8f3962a48b5cb79737)
## Table of contents
- [How to enforce TypeScript types in the runtime environment](#how-to-enforce-typescript-types-in-the-runtime-environment)
- [Table of contents](#table-of-contents)
- [Feature](#feature)
- [Usage](#usage)
- [Scripts](#scripts)
- [1. `yarn generate`](#1-yarn-generate)
- [Notes](#notes)
- [2. `yarn build`](#2-yarn-build)
- [3. `npx ts-node src/main.ts`](#3-npx-ts-node-srcmaints)
- [4. `node dist/main.js`](#4-node-distmainjs)
- [References](#references)
## 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](https://github.com/ForbesLindesay/typescript-json-validator/issues/36#issuecomment-977836986)
### 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

### 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.

## References
- [TypeScript](https://github.com/Microsoft/TypeScript)
- [TypeScript JSON Validator](https://github.com/ForbesLindesay/typescript-json-validator)