Skip to content

Instantly share code, notes, and snippets.

@matey-jack
Created November 6, 2017 10:35
Show Gist options
  • Save matey-jack/cf2962beb937720e472e1c2a8e1383b5 to your computer and use it in GitHub Desktop.
Save matey-jack/cf2962beb937720e472e1c2a8e1383b5 to your computer and use it in GitHub Desktop.
example: how to easily run any TypeScript code on your command line
#!/usr/bin/env ts-node
/*
You can run simple scripts in TypeScript without compiling or any other other
additional steps!
npm i -g typescript ts-node
chmod 755 test.ts
./test.ts
You can also test small code snippets on the command line:
ts-node -p "1+1"
Unfortunately I couldn't get the REPL to work.
ts-node
> 1+1
Thrown: ⨯ Unable to compile TypeScript
[eval].ts: Cannot find name 'exports'. (2304)
[eval].ts (0,11): Cannot find name 'module'. (2304)
*/
function foo( x : number ) {
return x * 2;
}
var y : number = 2;
console.log(`2×${y} = ${foo(y)}`);
// this gives the expected type error at compile time:
// console.log(`2×'a' = ${foo('a')}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment