@typeonly/loader
v0.6.0
Published
Brings types and interfaces from TypeScript at runtime.
Downloads
1,568
Readme
@typeonly/loader
TypeOnly aims to be the pure typing part of TypeScript. The TypeOnly parser generates a .to.json
file, which is a bundle that contains metadata extracted from .d.ts
typing files. Then, this package provides an API to read these RTO files. It brings typing metadata at runtime.
Tutorial: Load typing definitions at runtime
At first, it is necessary to follow the tutorial of TypeOnly in order to generate a .to.json
file based on your TypeScript definitions. After this step, you have the following file: dist/types.to.json
.
Now, add @typeonly/loader
to the project:
npm install @typeonly/loader
Create a file src/main.js
with the following content:
// src/main.js
const { loadModules, literals } = require("@typeonly/loader");
const modules = loadModules({
bundle: require(`./types.to.json`)
});
const { ColorName } = modules["./drawing"].namedTypes;
console.log("Color names:", literals(ColorName, "string"));
If you write this code in a TypeScript source file, simply replace the require
syntax with a standard import
.
We can execute our program:
$ node src/main.js
Color names: [ 'red', 'green', 'blue' ]
Yes, it’s as easy as it seems: the list of color names is now available at runtime.
Notice: The TypeOnly parser is used at build time. At runtime, our code only use @typeonly/loader
which is a lightweight wrapper for .to.json
files.
Contribute
With VS Code, our recommanded plugin is:
- TSLint from Microsoft (
ms-vscode.vscode-typescript-tslint-plugin
)