4.0.1 • Published 4 months ago

tsruntime v4.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
4 months ago

tsruntime

Build Status npm version

Typescript custom transformer for emitting types for using in runtime (reflection).

Motivation

Sometimes you need types metadata in the runtime, e.g. to validate backend response or set propTypes from interface Props{}

It's docs for v3. For v2 go here

Prerequisites

  • typescript: =>2.3.0

Setup

  • install tsruntime and ttypescript
  • change tsconfig.json:
{
    "compilerOptions": {
        "experimentalDecorators": true, // if you'll use decorators
        "plugins": [
            { "transform": "tsruntime/transformer.js", "type": "program" },
        ],
    }
}
  • run with ttypescript compiler:
    • ts-node --compiler=ttypescript src/index.ts
    • ttsc
    • change compiler in awesome-typescript-loader config
    • etc

See Example

Warning: You cannot use transpileOnly compiling mode and isolatedModules (if you want reflect types from imported modules).

Usage:

Using reflect function

import {reflect} from 'tsruntime';

interface StatsModel {
    a?: number
    b: string
    c: Array<string>
    d: number | string | null
}

const type = reflect<StatsModel>()
console.log(type)

const type2 = reflect<string>()

On compiled code you'll have

var type = tsruntime_1.reflect({
  kind: 15 /*Object*/,
  name: "StatsModel",
  properties: {
    a: {
      kind: 17 /*Union*/,
      types: [{ kind: 12 /*Undefined*/ }, { kind: 3 /*Number*/ }]
    },
    b: { kind: 2 /*String*/ },
    c: {
      kind: 18 /*Reference*/,
      type: Array,
      arguments: [{ kind: 2 /*String*/ }]
    },
    d: {
      kind: 17 /*Union*/,
      types: [
        { kind: 13 /*Null*/ },
        { kind: 2 /*String*/ },
        { kind: 3 /*Number*/ }
      ]
    }
  }
})();

Using class decorators

import {Reflective, getClassType} from 'tsruntime';

@Reflective
export class StatsModel {
    a?: number
    b!: string
    c!: Array<string>
    d!: number | string | null
}

@Reflective
class Foo extends Array<string> {

}

console.log(getClassType(StatsModel))
console.log(getClassType(Foo))

On runtime you'll have

// ...
StatsModel = __decorate(
    [
      tsruntime_1.Reflective({
        kind: 19 /*Class*/,
        name: "StatsModel",
        properties: {
          a: {
            kind: 17 /*Union*/,
            types: [{ kind: 12 /*Undefined*/ }, { kind: 3 /*Number*/ }]
          },
          b: { kind: 2 /*String*/ },
          c: {
            kind: 18 /*Reference*/,
            type: Array,
            arguments: [{ kind: 2 /*String*/ }]
          },
          d: {
            kind: 17 /*Union*/,
            types: [
              { kind: 13 /*Null*/ },
              { kind: 2 /*String*/ },
              { kind: 3 /*Number*/ }
            ]
          }
        }
      })
    ],
    StatsModel
  );
// ...

Using types info

import {Types, reflect} from 'tsruntime';

const isString = reflect<string>().kind === Types.TypeKind.String

Full runtime info available

Customization

You can customize both reflect and Reflective to do whatever you want

import {createReflective, Types} from 'tsruntime';

const storage = {} as any

function MyReflective (key: string) {
    return createReflective(reflectedType => {
        return (target: any) => {
            storage[key] = reflectedType
        }
    })
}

// typeof MyReflective('realcls') is MarkReflective type.

@MyReflective('realcls')
class Cls {
    prop = 42
}
// in compiled code - @MyReflective('realcls')({kind: ...})

const clsType = storage['realcls']


const validateResp = createReflective(
  reflectedType => <T>(resp: unknown) => { //should have <T>
    if (reflectedType.kind === Types.TypeKind.String) {
        return typeof resp === 'string'
    }
  }
)

const isValid = validateResp<string>('asd') 
// in compiled code - validateResp({kind: ...})('asd')

How it works

createReflective expects reflectedType => T to be passed as arg.

It returns T and marks it as reflectable (type MarkReflective).

Transformer see that symbol have type MarkReflective and calls it with reflectedType.

For function it gets type from first generic argument (thats why you need <T>), for decorator - from class declaration, related to decorator.

5.0.1

4 months ago

4.0.1

4 months ago

5.0.0

5 months ago

4.0.0

5 months ago

3.0.0-beta.1

5 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.4.0

7 years ago

1.3.1

7 years ago

1.2.7

7 years ago

1.3.0

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago