tinspector
TypeScript type inspector
Last updated 3 months ago by ktutnik .
MIT · Original npm · Tarball · package.json
$ cnpm install tinspector 
SYNC missed versions from official npm registry.

tinspector

ubuntu Coverage Status Greenkeeper badge

An introspection (reflection) library, extract JavaScript/TypeScript type into metadata information such as class name, methods, parameters and their appropriate data types

Example

import reflect from "tinspector"

class Awesome{
    awesome(){}
}

class MyAwesomeClass extends Awesome {
    @decorateMethod({ cache: "20s" })
    myAwesomeMethod(stringPar:string, numberPar:number): number {
        return Math.random()
    }
}

const metadata = reflect(MyAwesomeClass)

Result of metadata variable above is like below

{
    kind: 'Class',
    name: 'MyAwesomeClass',
    type: MyAwesomeClass,
    decorators: [],
    properties: [],
    ctor: {
        kind: 'Constructor',
        name: 'constructor',
        parameters: []
    },
    methods: [
        {
            kind: 'Method',
            name: 'myAwesomeMethod',
            parameters: [
                {
                    kind: 'Parameter',
                    name: 'stringPar',
                    type: String,
                    decorators: [],
                    properties: {}
                },
                {
                    kind: 'Parameter',
                    name: 'numberPar',
                    type: Number,
                    decorators: [],
                    properties: {}
                }
            ],
            decorators: [{ cache: '20s' }],
            returnType: Number,
        },
        {
            kind: 'Method',
            name: 'awesome',
            parameters: [],
            decorators: [],
            returnType: undefined,
        }
    ]
}

Features

  • [x] Inspect function
  • [x] Inspect module or file
  • [x] Inspect class
  • [x] Inspect class with inheritance
  • [x] Inspect getter and setter
  • [x] Inspect methods
  • [x] Inspect parameters
  • [x] Supported inspect destructured parameter
  • [x] Supported inspect rest parameter
  • [x] Supported inspect parameter with complex default value
  • [x] (TypeScript only) Inspect decorators
  • [x] (TypeScript only) Inspect parameter properties
  • [x] (TypeScript only) Inspect type information
  • [x] (TypeScript only) Configurable decorator (inheritable / allow multiple)
  • [x] (TypeScript only) Generic class inheritance

TypeScript Requirement

To be able to inspect type information its required to enable configuration below in tsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true           
  }
}

Inspect Type Information

TypeScript when emitDecoratorMetadata enabled, TypeScript will add type information during compile time. This make it possible to extract typescript type information during runtime.

CAVEAT

TypeScript emitDecoratorMetadata has some limitation.

  • Declaration should have at least one decorator to get metadata (type information)
  • Array element type information not included.
  • Generic type information not included.

Based on limitation above its required at least have one decorator to make tinspector to be able to extract type information:

import reflect from "tinspector"

class Awesome {
    @reflect.noop()
    awesome(multiply:number): number { return 1 }
}

const metadata = reflect(Awesome)

tinspector will be able to get type information of the method's return type and parameters of the awesome method above. Note that we applied @reflect.noop() decorator on the awesome method. @reflect.noop() does nothing except to force TypeScript to emit metadata information.

import reflect from "tinspector"

class Awesome {
    @reflect.noop()
    aweProperty:number
}

const metadata = reflect(Awesome)

Above code showing that we able to get type information of a property.

import reflect from "tinspector"

@reflect.noop()
class Awesome {
    constructor(multiply:number){}
}

const metadata = reflect(Awesome)

Above code showing that we able to get type information of parameters of the constructor, by applying decorator on the class level.

Inspect Array and Generic Type

To get type information of an Array and Generic type its required to use @reflect.type() decorator and provide the type on the callback parameter.

import reflect from "tinspector"

class Awesome {
    @reflect.type([Number])
    awesome(multiply:number): Array<number> {}
}

const metadata = reflect(Awesome)

Above code showing that we able to get method's return type information by providing @reflect.type([Number]). Note that the [Number] is an array of Number.

import reflect from "tinspector"

class Option { 
    data:string
}

class Awesome {
    @reflect.type(Option)
    awesome(multiply:number): Partial<Option> {}
}

const metadata = reflect(Awesome)

We will be able to get generic type information such as Partial, Required etc by applying @reflect.type() like above.

If you receive ReferenceError: <type name> is not defined error, thats mean you use @reflect.type(MyType) prior than its declaration. To solve this issue you can use callback @reflect.type(x => MyType) or @reflect.type(x => [MyType]) for array type.

Inspect Generic Class Information

With above trick its possible to get generic type information in a generic class members with some extra configuration below

@generic.template("T", "U")
class SuperAwesome<T, U> {
    awesome(@reflect.type("T") par: T, @reflect.type("U") par2: U) {}
}

@generic.type(Number, String)
class Awesome extends SuperAwesome<Number, String> { }

const metadata = reflect(Awesome)

Above code showing that we add a specialized decorator @generic.template() to define generic template type. We also defined data type of the generic parameters using @reflect.type() decorator. Next on the inherited class we specify @generic.type() to define types replace the generic template. Note that the order of the parameter on @generic.template() and @generic.type() is important.

Inspect Parameter Properties

TypeScript has parameter properties feature, which make it possible to use constructor parameter as property. tinspector able to extract parameter properties type information by using @reflect.parameterProperties() decorator.

import reflect from "tinspector"

@reflect.parameterProperties()
class Awesome {
    constructor(public multiply:number){}
}

const metadata = reflect(Awesome)

Custom Decorator

Tinspector able to extract classes/methods/properties/parameters decorated with predefined decorators. There are predefined decorators should be use to be able for Tinspector to inspect the decorators

Decorator Description
decorateClass Decorate class with object specified in parameter
decorateProperty Decorate property with object specified in parameter
decorateMethod Decorate method with object specified in parameter
decorateParameter Decorate parameter with object specified in parameter
decorate Decorate any (class, property, method, parameter) with object specified in parameter
mergeDecorator Merge multiple decorators into one, useful on creating custom decorator

Example usage

import { decorateClass, decorateProperty } from "tinspector"

@decorateClass({ message: "hello world" })
class Awesome {
    @decorateProperty({ message: "awesome!" })
    awesome: number = 10
}

Parameter passed on each decorator can be any object contains value, methods etc, those value will be returned when the class metadata extracted.

Create your own custom decorator by creating function returns decorator above

import { decorateMethod } from "tinspector"

// create custom decorator
function cache(duration:number){
    return decorateMethod({ type: "Cache", duration })
}

class Awesome{
    @cache() // use it like usual decorator
    awesome(){}
}

Use mergeDecorator to combine multiple decorator on custom decorator

import { decorateMethod, mergeDecorator } from "tinspector"

// create custom decorator
function cacheAndDelay(duration:number){
    return mergeDecorator([
        decorateMethod({ type: "Cache", duration }), 
        decorateMethod({ type: "Delay", duration })
    ])
}

Decorator Option (Inheritance, Allow Multiple)

Decorator can be further configured to match the behavior you need like below.

decorateMethod(<data>, <option>)

Option is a simple object with properties:

  • inherit Boolean If false decorator will not be merged on the derived class. Default true
  • allowMultiple Boolean If false throw error when multiple decorator applied on class. Also when set false will prevent super class decorator being merged into derived class when already exists. When set false, decorator required to have DecoratorId property to identify the similar decorator.

Example disable decorator inheritance

@decorateClass({ log:true }, { inherit: false })
class Awesome {
    awesome(){}
}

// { log: true} will not inherited on this class
class IamAwesome extends Awesome{ }

Example disable multiple decorator on inheritance

import { DecoratorId, decorateClass } from "tinspector"

function log(){
    return @decorateClass({ [DecoratorId]: "logging",  log:true }, { allowMultiple: false })
}

@log()
class Awesome {
    awesome(){}
}

// parent decorator will not merged
// guaranteed derived class only have single decorator with specific ID
@log()
class IamAwesome extends Awesome{ }

Flush Cache

By default reflect process cached globally for performance reason. But in some case if you modify the class preferences by adding a new decorator etc, your new update will not returned until you flush the cache.

const metadata = reflect(MyAwesomeClass, { flushCache: true })

Current Tags

  • 3.2.3-canary.kfku9xyl.0                                ...           canary (13 hours ago)
  • 3.2.2                                ...           latest (3 months ago)

122 Versions

  • 3.2.3-canary.kfku9xyl.0                                ...           13 hours ago
  • 3.2.3-canary.kfg7x8ie.0                                ...           4 days ago
  • 3.2.3-canary.kfdk5pvu.0                                ...           6 days ago
  • 3.2.3-canary.kf9he5u8.0                                ...           8 days ago
  • 3.2.3-canary.kf8yuiqx.0                                ...           9 days ago
  • 3.2.3-canary.kf83flsl.0                                ...           9 days ago
  • 3.2.3-canary.kf6ox8zq.0                                ...           10 days ago
  • 3.2.3-canary.kf6lwncy.0                                ...           10 days ago
  • 3.2.3-canary.kf6gpx8m.0                                ...           11 days ago
  • 3.2.3-canary.kf3gt31v.0                                ...           13 days ago
  • 3.2.3-canary.kf32nfdw.0                                ...           13 days ago
  • 3.2.3-canary.kf1ngfd8.0                                ...           14 days ago
  • 3.2.3-canary.kezj7wpq.0                                ...           15 days ago
  • 3.2.3-canary.kezbpkd4.0                                ...           16 days ago
  • 3.2.3-canary.keyz518l.0                                ...           16 days ago
  • 3.2.3-canary.key2i6eu.0                                ...           16 days ago
  • 3.2.3-canary.kerx6yap.0                                ...           21 days ago
  • 3.2.3-canary.keqn8ee3.0                                ...           22 days ago
  • 3.2.3-canary.kepjwxp9.0                                ...           22 days ago
  • 3.2.3-canary.keoswy6l.0                                ...           23 days ago
  • 3.2.3-canary.kenou8nl.0                                ...           24 days ago
  • 3.2.3-canary.kela2rem.0                                ...           25 days ago
  • 3.2.3-canary.kel7fsyz.0                                ...           25 days ago
  • 3.2.3-canary.kejh0j48.0                                ...           a month ago
  • 3.2.3-canary.kefh7qik.0                                ...           a month ago
  • 3.2.3-canary.kef5312a.0                                ...           a month ago
  • 3.2.3-canary.kef2e3lg.0                                ...           a month ago
  • 3.2.3-canary.keexw8ts.0                                ...           a month ago
  • 3.2.3-canary.keeqwum9.0                                ...           a month ago
  • 3.2.3-canary.kedxkqhh.0                                ...           a month ago
  • 3.2.3-canary.kebz3sj5.0                                ...           a month ago
  • 3.2.3-canary.keaththw.0                                ...           a month ago
  • 3.2.3-canary.keao8m4o.0                                ...           a month ago
  • 3.2.3-canary.ke93q61k.0                                ...           a month ago
  • 3.2.3-canary.ke91wrl1.0                                ...           a month ago
  • 3.2.3-canary.ke8zqwdv.0                                ...           a month ago
  • 3.2.3-canary.kdvw2za0.0                                ...           a month ago
  • 3.2.3-canary.kdtibqoo.0                                ...           a month ago
  • 3.2.3-canary.kdtej428.0                                ...           a month ago
  • 3.2.3-canary.kdqpr7yp.0                                ...           2 months ago
  • 3.2.3-canary.kdkqxkys.0                                ...           2 months ago
  • 3.2.3-canary.kdjj7hrf.0                                ...           2 months ago
  • 3.2.3-canary.kdjdfg7t.0                                ...           2 months ago
  • 3.2.3-canary.kdid6yqc.0                                ...           2 months ago
  • 3.2.3-canary.kdi9bq15.0                                ...           2 months ago
  • 3.2.3-canary.kdi5mwkd.0                                ...           2 months ago
  • 3.2.3-canary.kdgfdc3a.0                                ...           2 months ago
  • 3.2.3-canary.kdf0lwtn.0                                ...           2 months ago
  • 3.2.3-canary.kd6wewq6.0                                ...           2 months ago
  • 3.2.3-canary.kd3rqvd4.0                                ...           2 months ago
  • 3.2.3-canary.kd3mpv1u.0                                ...           2 months ago
  • 3.2.3-canary.kd2p0nhi.0                                ...           2 months ago
  • 3.2.3-canary.kd1i13g6.0                                ...           2 months ago
  • 3.2.3-canary.kd18a18n.0                                ...           2 months ago
  • 3.2.3-canary.kd13oe59.0                                ...           2 months ago
  • 3.2.3-canary.kd1392pj.0                                ...           2 months ago
  • 3.2.3-canary.kcwgd771.0                                ...           2 months ago
  • 3.2.3-canary.kcvksj6x.0                                ...           2 months ago
  • 3.2.3-canary.kctvam2m.0                                ...           2 months ago
  • 3.2.3-canary.kcs8mlx5.0                                ...           2 months ago
  • 3.2.3-canary.kcs5b8ny.0                                ...           2 months ago
  • 3.2.3-canary.kcrinrbo.0                                ...           2 months ago
  • 3.2.3-canary.kcras9pq.0                                ...           2 months ago
  • 3.2.3-canary.kckaf5hv.0                                ...           2 months ago
  • 3.2.3-canary.kciolr74.0                                ...           3 months ago
  • 3.2.3-canary.kc7ailpb.0                                ...           3 months ago
  • 3.2.3-canary.kc6zqei2.0                                ...           3 months ago
  • 3.2.3-canary.kc6u1s8b.0                                ...           3 months ago
  • 3.2.3-canary.kc1pwqwc.0                                ...           3 months ago
  • 3.2.3-canary.kc1j62zf.0                                ...           3 months ago
  • 3.2.3-canary.kc1bv7t9.0                                ...           3 months ago
  • 3.2.3-canary.kbyvfeer.0                                ...           3 months ago
  • 3.2.3-canary.kbyejgob.0                                ...           3 months ago
  • 3.2.3-canary.kby6baek.0                                ...           3 months ago
  • 3.2.3-canary.kbx8uhbt.0                                ...           3 months ago
  • 3.2.3-canary.kbwy7dnv.0                                ...           3 months ago
  • 3.2.3-canary.kbwxgwmn.0                                ...           3 months ago
  • 3.2.3-canary.kbwv88am.0                                ...           3 months ago
  • 3.2.3-canary1593125083.0                                ...           3 months ago
  • 3.2.3-canary1593069049.0                                ...           3 months ago
  • 3.2.3-canary1593026645.0                                ...           3 months ago
  • 3.2.3-canary1592886862.0                                ...           3 months ago
  • 3.2.3-canary1592877058.0                                ...           3 months ago
  • 3.2.3-canary1592806142.0                                ...           3 months ago
  • 3.2.2                                ...           3 months ago
  • 3.2.1                                ...           3 months ago
  • 3.2.0                                ...           4 months ago
  • 3.1.2                                ...           4 months ago
  • 3.1.1                                ...           4 months ago
  • 3.1.0                                ...           4 months ago
  • 3.0.8                                ...           4 months ago
  • 3.0.7                                ...           4 months ago
  • 3.0.6                                ...           4 months ago
  • 3.0.5                                ...           4 months ago
  • 3.0.4                                ...           5 months ago
  • 3.0.3                                ...           5 months ago
  • 3.0.2                                ...           5 months ago
  • 3.0.1                                ...           5 months ago
  • 3.0.0                                ...           5 months ago
  • 2.3.1                                ...           6 months ago
  • 2.3.0                                ...           7 months ago
  • 2.2.10                                ...           8 months ago
  • 2.2.9                                ...           10 months ago
  • 2.2.8                                ...           10 months ago
  • 2.2.7                                ...           a year ago
  • 2.2.6                                ...           a year ago
  • 2.2.5                                ...           a year ago
  • 2.2.3                                ...           a year ago
  • 2.2.2                                ...           a year ago
  • 2.2.1                                ...           a year ago
  • 2.2.0                                ...           2 years ago
  • 2.1.2                                ...           2 years ago
  • 2.1.1                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 2.0.0-3                                ...           2 years ago
  • 2.0.0-2                                ...           2 years ago
  • 2.0.0-1                                ...           2 years ago
  • 2.0.0-0                                ...           2 years ago
  • 1.4.0-alpha.caa0578b                                ...           2 years ago
  • 1.4.0-alpha.79af24d3                                ...           2 years ago
  • 1.4.0-alpha.b123904d                                ...           2 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 369
Last Day 29
Last Week 103
Last Month 425
Dependencies (3)
Dev Dependencies (6)

Copyright 2014 - 2016 © taobao.org |