TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS. Open source.
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.
TypeScript is JavaScript with an important difference. TypeScript is typed.
Types allow TypeScript to offer key benefits like code completion, refactoring, and type checking.
Types also make it easier to document code. And, when used with an IDE like Visual Studio Code, types can provide a richer development experience by offering code completion and inline documentation.
Node.js is a platform for building fast and scalable network applications.
Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
To get started with TypeScript and Node.js, you will need to install Node.js.
Once Node.js is installed, you can create a TypeScript file and compile it to JavaScript using the tsc
command line tool.
// greeter.ts
function greeter(person: string) {
return "Hello, " + person;
}
let user = "Jane User";
document.body.textContent = greeter(user);
tsc greeter.ts
This will create a file called greeter.js
that contains the compiled JavaScript.
To run the compiled JavaScript, you will need to use a Node.js runtime.
node greeter.js
You can also use TypeScript with existing JavaScript code.
// greeter.ts
function greeter(person: string) {
return "Hello, " + person;
}
let user = "Jane User";
document.body.textContent = greeter(user);
// greeter.js
function greeter(person) {
return "Hello, " + person;
}
let user = "Jane User";
document.body.textContent = greeter(user);