[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

🎩 Use Your TypeScript definitions at runtime. Powered by Manta Style.

License

Notifications You must be signed in to change notification settings

Cryrivers/magic-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magic Type

🎩 Use Your TypeScript definitions at runtime. Powered by Manta Style.

version GitHub


Installation

  • With Yarn
yarn global add magic-type
  • With NPM
npm install -g magic-type

You could also use Magic Type locally as well.

Usage

Let's take the following code as an example.

// typings/api.ts
export type MyAPIRequest = {
  time: number;
  input: string;
};

Compile TypeScript definitions

asciicast

Usage: magictype [options]

Options:
  -V, --version          output the version number
  -i --inputFile <file>  The entrypoint of files to be compiled
  -o --outputDir <dir>   The output directory of compiled files
  -c --cjs               Transpile modules to CommonJS (Otherwise it will output ES modules)
  -h, --help             output usage information

Use magic types at runtime

import { MyAPIRequest } from "./api";

// Use Magic Types as normal TypeScript types
type MyType = {
  key: MyAPIRequest;
};

// Use Magic Type at Runtime

// Check if user input matches MyAPIRequest
function checkMyInput(input: any) {
  return MyAPIRequest.validate(input);
}

// Format user input to match MyAPIRequest
function formatMyInput(input: any) {
  return MyAPIRequest.format(input);
}

// Generate a mock MyAPIRequest data
function getMockData() {
  return MyAPIRequest.deriveLiteral([]).mock();
}
  • Developer Experience with TypeScript's type guard Developer Experience

  • Runtime Screenshot: Runtime Screenshot

Unsupported Syntax

Please be noted there's no warnings against unsupported syntax yet (And Magic Type may crash as well). Please kindly check by yourself.

  • extends keyword in interface declaration will be ignored.
  • Union (A | B) and intersection(A & B) on index signatures are not supported.
  • Functions
  • infer keyword
  • ReturnType<T> and InstanceType<T> are not supported due to unsupported infer keyword and functions.

License

Magic Type is MIT licensed

About

🎩 Use Your TypeScript definitions at runtime. Powered by Manta Style.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published