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
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.
// Using ES6 imports
import transformer from 'ts-type-checked/transformer';
// Or using the old syntax
const transformer = require('ts-type-checked/transformer').default;
{
test: /\.ts(x)?$/,
loader: 'ts-loader', // Or 'awesome-typescript-loader'
options: {
getCustomTransformers: program => ({
before: [transformer(program)],
}),
},
}
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.
import transformer from 'ts-type-checked/transformer';
import ts from '@wessberg/rollup-plugin-ts';
// ...
ts({
transformers: [
({ program }) => ({
before: transformer(program),
}),
],
}),
import typescript from 'rollup-plugin-typescript2';
// ...
typescript({
transformers: [
service => ({
before: [transformer(service.getProgram())],
after: [],
}),
],
}),
# NPM
npm install --dev ttypescript
# Yarn
yarn add -D ttypescript
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" }
]
}
}
In order to enable ts-type-checked
in your Jest tests you need to switch to ttypescript
compiler.
In your jest.config.js
(or package.json
):
module.exports = {
preset: 'ts-jest',
globals: {
'ts-jest': {
compiler: 'ttypescript',
},
},
};
In order to enable ts-type-checked
in your Jest tests you need to switch to ttypescript
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',
});
Either using command line:
$ ts-node --compiler ttypescript ...
Or the programmatic API:
require('ts-node').register({
compiler: 'ttypescript'
})