Convert an existing project to TypeScript?
Rename files from javascript to typescript
ls *.js -Recurse | foreach{ move-item $_ ($_.FullName.TrimEnd("js") + "ts") }
install grunt-typescript (There are other TypeScript packages out there but I tried this one and it worked)
npm install grunt-typescript --save-dev
typescript: {
base: {
src: ['lib/**/*.ts'],
options: {
target: 'es5',
module: 'commonjs'
}
}
},
grunt.loadNpmTasks('grunt-typescript');
grunt.registerTask('default', ['typescript', 'test', 'jshint']);
Ignore files generated by TypeScript
install a reference to the node type definitions
tsd install node
create a common references file.
new-item -path ./lib/references.ts -type file -value '/// <reference path="../d.ts/DefinitelyTyped/node/node.d.ts" />'
prepend each typescript file with a link to the references file.
Put the references.ts path into the root of each file.
$projectRoot = (pwd).path;
ls .\lib\* - exclude references.ts - Include * .ts - Recurse | % { $subCount = ($_.Directory.FullName ).replace($projectRoot + ' \' , ' ' ).split(' \' ).length- 1 ; $file = cat $_ ; sc $_ - value (" ///<reference path='" + (" ../" * $subCount ) + " references.ts' />" + [System.Environment ]::NewLine), $file }
remove all the lib js files and prove it still works.
rm .\lib\* -Recurse -Include *.js
Does it actually transpile all the JS code into TS code ?