Documentation chapters
Type

Getting Started

This is the documentation for Deepkit Type, a high-performance serializer, validator, change detector, and reflection API for TypeScript. It the first solution on the market that makes the full set of TypeScript types available in the JavaScript runtime. By providing a rich reflection system library authors can build upon the emitted TypeScript types and build solutions that are deeply intertwined with TypeScript types.

Installation

npm install @deepkit/type @deepkit/type-compiler

Runtime type information are not generated by default. Set "reflection": true in the tsconfig.json to enable it in all files in the same folder of the tsconfig.json or in sub folders.

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "CommonJS",
    "target": "es6",
    "moduleResolution": "node",
    "experimentalDecorators": true,
  },
  "reflection": true
}

If you want to use decorators, you should enable experimentalDecorators in the tsconfig.json. This is not necessarily required to work with @deepkit/type, but for certain features in @deepkit/framework.

Type compiler

To make type information available in runtime, Deepkit uses a type compiler in the package @deepkit/type-compiler. It is a TypeScript transformer that reads your TypeScript and embeds the type information directly in the emitted JavaScript. To make this work, you need to install @deepkit/type-compiler.

The transformer is automatically injected per default in the locally installed TypeScript (usually at node_modules/typescript). By automatically injecting it in your local typescript dependency, the transformer is executed automatically in your angular builds, tsc builds, webpack, ts-node, and much more. To inject the transformer manually, run:

node_modules/.bin/deepkit-type-install

Note that you have to run deepkit-type-install if you updated your local typescript version (for example when your typescript version in package.json changed and you run npm install).

To remove the injected transformer, run the following command:

node_modules/.bin/deepkit-type-uninstall

Note that you have to setup the transformer now manually in your build setup or you won't have type information in the runtime available.

Webpack

If you want to use the type compiler in your webpack build, you can do so using the package ts-loader (or any other typescript loader that supports registering transformers).

const typeCompiler = require('@deepkit/type-compiler');

module.exports = {
    entry: './app.ts',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: {
                    loader: 'ts-loader',
                    options: {
                        //this enables @deepkit/type's type compiler
                        getCustomTransformers: (program, getProgram) => ({
                            before: [typeCompiler.transformer],
                            afterDeclarations: [typeCompiler.declarationTransformer],
                        }),
                    }
                },
                exclude: /node_modules/,
            },
        ],
    },
}

Using

import { deserialize } from '@deepkit/type';

class MyModel {
    id: number = 0;

    created: Date = new Date;

    constructor(public name: string) {
    }
}

const myModel = deserialize<MyModel>({
    id: 23,
    created: 'Sat Oct 13 2018 14:17:35 GMT+0200',
    name: 'Peter',
});

console.log('Is MyModel?', myModel instanceof MyModel);
console.log('Date converted?', myModel.created instanceof Date);
console.log(myModel);
You can run this script using ts-node app.ts.
$ ts-node app.ts
Is MyModel? true
Date converted? true
MyModel {
  name: 'Peter',
  id: 23,
  created: 2018-10-13T12:17:35.000Z
}
Made in Germany