Skip to content

Instantly share code, notes, and snippets.

@staxmanade
Last active February 14, 2019 06:56
Show Gist options
  • Save staxmanade/6414463 to your computer and use it in GitHub Desktop.
Save staxmanade/6414463 to your computer and use it in GitHub Desktop.
Some notes on converting JS to TypeScript with the help of PowerShell

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

update gruntfile

1

typescript: {
    base: {
        src: ['lib/**/*.ts'],
        options: {
            target: 'es5',
            module: 'commonjs'
        }
    }
},

2

grunt.loadNpmTasks('grunt-typescript');

3

grunt.registerTask('default', ['typescript', 'test', 'jshint']);

run 'grunt'

Ignore files generated by TypeScript

npm install -g tsd

or Nuget to download

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
@GhostGumm
Copy link

Does it actually transpile all the JS code into TS code ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment