Compiler Setup
How It Works
TypeScript or Babel provides a compiler:
tsc
(TypeScript compiler)babel
(Babel compiler)
Compiles
- newer language features of TypeScript or
ES2015
and beyond - to earlier versions of JavaScript (commonly
ES5
) that are supported in web browsers (where the application will be running)
- newer language features of TypeScript or
The compilation is often more specificaly referred to as transpilation.
- Transpilation refers to a specific kind of compilation, source code to source code.
- We traditionally think about compilers as transforming source code to bytecode.
To see this in action:
- Visit: Babel REPL
- or
- Visit: TypeScript Playground
- Visit: Babel REPL
Setup (Babel)
Create Project
mkdir esdemos
cd esdemos
code . //opens Visual Studio Code
Install Babel
Open a command-prompt or terminal
Change directory to
esdemos
Run these commands to install
babel
packages:npm init -y
npm install --save-dev @babel/core @babel/cli @babel/preset-envCreate a config file named
babel.config.json
in the root of your project with this content:babel.config.json
{
"presets": ["@babel/preset-env"]
}Create a config file name
.browserslistrc
in the root of the project with the following content. This file defines what browsers we will support in our application..browserslistrc
> 0.25%,
not deadRun the following command to see which browsers will be supported.
npx browserslist
Run Babel
- Create file
src\program.js
- Add the following code:
const greeting = "hello";
console.log(greeting);
- Open a
command-prompt
orterminal
. - Set the
current
directory toesdemos
. - Run this command to compile all your code from the
src
directory tolib
:
npx babel src --watch --out-dir lib
npx
allows you to run local packages and is available in npm 5.2 or higher. For more information see this article
- Open another (a new) command-prompt or terminal in the
esdemos
directory.
- In VS Code: View> Integrated Terminal
- Click the + or the split terminal icon
- Run the command:
node ./lib/program.js
- Result:
hello
Setup (TypeScript)
Create Project
mkdir typescriptdemo
cd typescriptdemo
code . //opens Visual Studio Code
Install TypeScript
In a command-prompt or terminal
npm init -y
npm install typescript@4 --save-dev
Configure TypeScript
Open a
command-prompt
orterminal
.Set the
current
directory totypescriptdemo
.Run the command:
npx tsc --init
This creates a
tsconfig.json
file with the default command line options.Documentation for all TypeScript compiler options are available here.
Setting the Compiler Strictness
Open
tsconfig.json
and change thestrict
setting tofalse
. Also, setnoEmitOnError
totrue
/* Strict Type-Checking Options */
"strict": false,
"noEmitOnError": true,
...--strict
Enabling --strict enables --noImplicitAny, --noImplicitThis, --alwaysStrict, --strictBindCallApply, --strictNullChecks, --strictFunctionTypes and --strictPropertyInitialization.
--strictNullChecks
In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to themselves and any (the one exception being that undefined is also assignable to void).
The default setting for the compiler option
--noEmitOnError
is false so output is emitted even if errors were reported. For more information on why this is the default behavior see: https://github.com/Microsoft/TypeScript/issues/828
Run TypeScript
Create file
program.ts
Code:
function greeter(name) {
console.log("Hi " + name);
}
greeter("Ben");Run the command
npx tsc -w
Open another (a new) command-prompt or terminal in the
typescriptdemo
directory.
- In VS Code: View> Integrated Terminal
- Click the + or the split terminal icon
Run the command:
node program.js
Result:
Hi Ben