On Github nicojs / typescript-tips-and-tricks
tsc --init
{ "compilerOptions": { "module": "commonjs", "target": "es5", "noImplicitAny": true, "sourceMap": true, "outDir": "dist" }, "exclude": [ "node_modules" ] }
// Grunt module.exports = function (grunt) { grunt.initConfig({ ts: { build: { tsconfig: true } } }
// Gulp var ts = require('gulp-typescript'); gulp.task('default', function () { return gulp.src('src/**/*.ts') .pipe(ts()) .pipe(gulp.dest('dist')); });
// Package.json { "scripts": { "start": "tsc" } }
# MSBuild PM> Install-Package Microsoft.TypeScript.MSBuild
import * as _ from 'lodash'; export class ArrayWrapper<T> { constructor(private arr: T[]){ } last(){ return _.last(this.arr); } }
{ "module": "commonjs", "target": "es5" }
// tsconfig.json { "compilerOptions": { "declaration": true, // ... } }
// Package.json { "main": "./lib/foo.js", "typings": "./lib/foo.d.ts" }
// tsconfig.json { "compilerOptions": { "moduleResolution": "node", // ... } }
// file.ts import * as foo from 'foo'; foo.bar();// use the library
// Library package.json { "main": "./lib/foo.js", "typings": "./lib/foo.d.ts" "main": "./foo.js" }
# move files to root of your module $ ls node_modules/foo foo.d.ts foo.js foo.ts
// Consumer import * as foo from 'foo/foo'; foo.bar();// use the library
any-type can provide flexibility
// MutantRunResultMatcher.ts import Mutant from './Mutant'; export default class MutantRunResultMatcher { constructor(mutants: Mutant[], results: RunResult[]) { } // some realy complex stuff }
// MutantRunResultMatcherSpec.ts import Mutant from './Mutant'; import MutantRunResultMatcher from './MutantRunResultMatcher'; describe('MutantRunResultMatcher', () => { let mutants: any[]; // Flexibility let results: any[]; beforeEach(() => { mutants = []; results = []; sut = new MutantRunResultMatcher(mutants, runResultsByTestId); });
TypeScript is flexible. Make sure you work together in your team