Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@typeonly/loader

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@typeonly/loader

Brings types and interfaces from TypeScript at runtime.

  • 0.6.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
402
increased by57.65%
Maintainers
2
Weekly downloads
 
Created
Source

@typeonly/loader

Build Status npm Type definitions GitHub

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)

Keywords

FAQs

Package last updated on 13 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc