☝️ TypeScript plugin providing access to type information at runtime
MIT License
TypeScript transformation plugin that provides access to TypeScript type information at runtime. Comes with a validation function and a type-checking JSON.parse()
.
Use with TypeScript 3.
⚠️ Status: Experimental
$ npm install type-reflect
Accessing type information in your code is easy:
import Reflect from "type-reflect"
interface User {
name: string,
role: "admin" | "read-write" | "read-only"
}
export const schema = Reflect<User>()
The Reflect<>()
call above will be transpiled to:
export const schema = {
type: "object",
title: "User",
properties: {
name: {
type: "string"
},
role: {
type: "string",
enum: ["admin", "read-write", "read-only"]
}
},
required: ["id", "name", "role"]
}
It works with all kinds of types, like strings, any
, objects, arrays and much more:
const ageSchema = Reflect<"new" | "old">()
import { validate } from "type-reflect/validate"
import { getUser } from "./user"
import { schema } from "./user-schema"
const user = validate(getUser(), schema)
// Throws if the return value of `getUser()` does not match the schema
// TypeScript can automatically infer that `user` is of type `User`
import { parseJSON } from "type-reflect/validate"
import { schema } from "./user-schema"
const json = `{
"name": "Me",
"role": "read-write"
}`
const user = parseJSON(json, schema)
// Throws if the JSON data does not match the schema
// TypeScript can automatically infer that `user` is of type `User`
To transpile the TypeScript code using the type-reflect
transformation, use the ts
compiler:
$ ts --transform type-reflect/transform ./source.ts
To permanently use the transformation in your project, add this configuration to your package.json
:
{
// ...
"ts": {
"transforms": [
"type-reflect"
]
}
}
Note that you cannot use the standard tsc
, since it does not expose the transformation API.
any[]
right nowMIT