1.6.2 • Published 4 years ago

ts-transformer-interface v1.6.2

Weekly downloads
22
License
MIT
Repository
github
Last release
4 years ago

ts-transformer-interface

A TypeScript custom transformer generating runtime interface info. This is mainly for the runtime JSON schema validation. So only a subset of TypeScript types are handled. They're

  1. primitive types (string, number, boolean, etc)
  2. special types (null, any, unknown)
  3. type reference (other interface/class)
  4. array of (primitive type | type reference)

Example

Input

import { schema } from 'ts-transformer-interface';

interface Location {
  lat: number;
  lng: number;
}

interface User {
  name: string; // required primitive
  title?: string; // optional primitive
  houses: string[]; // required array
  location: Location; // type reference
  spouse?: User; // optional type reference
  children: User[]; // type reference array
  previousLocations?: Location[]; // optional type reference array
  referrer: User | string; // union type not supported, will become null
}

const userSchema = schema<User>();

Output

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var index_1 = require('../index');
var userSchema = {
  name: 'User',
  props: [
    { name: 'name', optional: false, type: 'string' },
    { name: 'title', optional: true, type: 'string' },
    { name: 'houses', optional: false, type: { arrayElementType: 'string' } },
    { name: 'location', optional: false, type: { referenceName: 'Location' } },
    { name: 'spouse', optional: true, type: { referenceName: 'User' } },
    { name: 'children', optional: false, type: { arrayElementType: { referenceName: 'User' } } },
    {
      name: 'previousLocations',
      optional: true,
      type: { arrayElementType: { referenceName: 'Location' } },
    },
    { name: 'referrer', optional: false, type: null },
  ],
};

Generic types (WIP)

Type definition

interface Box<T> {
  // generic type 1
  data: T;
}

interface BigBox<T> {
  // generic type with parameterized type
  box: Box<T>;
}

// NOTE: since we need a concrete type here so we need to parameterize with `any` as placeholder
const boxSchema = schema<Box<any>>();
const bigboxSchema = schema<BigBox<any>>();

output

var boxSchema = {
  name: 'Box',
  props: [
    {
      name: 'data',
      optional: false,
      type: { genericParameterName: 'T', genericParameterType: { referenceName: 'T' } },
    },
  ],
};

var bigboxSchema = {
  name: 'BigBox',
  props: [
    {
      name: 'box',
      optional: false,
      type: {
        genericParameterName: 'T',
        genericParameterType: { selfType: 'Box', typeArgumentType: { referenceName: 'T' } },
      },
    },
  ],
};

Concrete generic types

interface User {
  // ...
  box: Box<Box<User[]>>; // parameterized type
}

output

{
  name: "User",
  props: [
    // ...
    {
      name: 'box',
      optional: false,
      type: {
        selfType: 'Box',
        typeArgumentType: {
          selfType: 'Box',
          typeArgumentType: { arrayElementType: { referenceName: 'User' } },
        },
      },
    }
  ]
}

Reads as: box if of type a Box of a Box of an Array of User.

Installation

ttypescript

I personally use ts-patch to install all the transformers.

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "plugins": [{ "transform": "ts-transformer-interface/transformer" }]
  }
  // ...
}
1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.0

4 years ago