Skip to content

Latest commit

 

History

History
213 lines (152 loc) · 5.01 KB

INSTALLATION.md

File metadata and controls

213 lines (152 loc) · 5.01 KB

ts-type-checked

< Back to project

Installation

Webpack | Rollup | ttypescript | Jest | Mocha | ts-node

ts-type-checked is a TypeScript transformer - it generates the required type checks and injects them into your code at compile time. It is compatible with Webpack, Rollup, and ttypescript projects and works nicely with Jest, Mocha and ts-node.

You will first need to install ts-type-checked using npm, yarn or similar:

# NPM
npm install --dev ts-type-checked

# Yarn
yarn add -D ts-type-checked

Webpack

See example here

In order to enable ts-type-checked in your Webpack project you need to configure ts-loader or awesome-typescript-loader in you Webpack config.

1. Import the transformer

// Using ES6 imports
import transformer from 'ts-type-checked/transformer';

// Or using the old syntax
const transformer = require('ts-type-checked/transformer').default;

2. Adjust your ts-loader / awesome-typescript-loader configuration

{
  test: /\.ts(x)?$/,
  loader: 'ts-loader', // Or 'awesome-typescript-loader'
  options: {
    getCustomTransformers: program => ({
      before: [transformer(program)],
    }),
  },
}

Rollup

See example here

In order to enable ts-type-checked in your Rollup project you need to configure ts-loader or awesome-typescript-loader in you rollup config.

1. Import the transformer

import transformer from 'ts-type-checked/transformer';

2. Option 1: Adjust your @wessberg/rollup-plugin-ts plugin configuration

import ts from '@wessberg/rollup-plugin-ts';

// ...

ts({
  transformers: [
    ({ program }) => ({
      before: transformer(program),
    }),
  ],
}),

2. Option 2: Adjust your rollup-plugin-typescript2 plugin configuration

import typescript from 'rollup-plugin-typescript2';

// ...

typescript({
  transformers: [
    service => ({
      before: [transformer(service.getProgram())],
      after: [],
    }),
  ],
}),

TTypeScript

See example here

1. Install ttypescript

# NPM
npm install --dev ttypescript

# Yarn
yarn add -D ttypescript

2. Add ts-type-checked transformer

In order to enable ts-type-checked in your TTypescript project you need to configure plugins in your tsconfig.json.

{
  "compilerOptions": {
    "plugins": [
      { "transform": "ts-type-checked/transformer" }
    ]
  }
}

Jest

See example here

In order to enable ts-type-checked in your Jest tests you need to switch to ttypescript compiler.

1. Configure ttypescript

See the instructions above.

2. Set ttypescript as your compiler

In your jest.config.js (or package.json):

module.exports = {
  preset: 'ts-jest',
  globals: {
    'ts-jest': {
      compiler: 'ttypescript',
    },
  },
};

Mocha

See example here

In order to enable ts-type-checked in your Jest tests you need to switch to ttypescript compiler.

1. Configure ttypescript

See the instructions above.

2. Set ttypescript as your compiler

In your mocha.setup.js (or the place where you are registering ts-node for mocha):

require('ts-node').register({
  compiler: 'ttypescript',
  project: './tsconfig.json',
});

ts-node

See example here

1. Configure ttypescript

See the instructions above.

2. Set ttypescript as your compiler

Either using command line:

$ ts-node --compiler ttypescript ...

Or the programmatic API:

require('ts-node').register({
  compiler: 'ttypescript'
})