src
- has the TypeScript source filesdist
- where javascript that TypeScript "compiles" to go.node_modules
- where the npm packages are installed to.package.json
- file that defines the packagetsconfig.json
- file that with options for typescript's compiler
"devDependencies": {
"@types/node": "^10.12.21",
"ts-node": "^8.0.2",
"typescript": "^3.3.1"
}
The dependencies needed to create the final package.
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"example": "ts-node src/index.ts"
},
npm run build
compiles the TypeScript insrc
and puts itdist
.npm run watch
will recompile when a file changes.npm run example
compiles and runs TypeScript filesrc/index.ts
.
{
"compileOnSave": true, <-- editors with typescript support will recompile on save
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "dom"],
"noImplicitAny" : false, <--- Example option for how strict the compiler is
"outDir": "dist", <--- Where to put the output files
"declaration": true, <--- Creates '.d.ts' file, which define the types of the generated javascript
"target": "es5" <--- Which version of EMCAScript to compile to.
},
"include": [ <-- files to becompiled
"src/**/*"
]
}
To get started install the needed packages with npm install
.
To run a javascript file:
node dist/index.js
or to compile and run the TypeScript, install ts-node
globally. npm install -g ts-node
ts-node src/index.ts
Note ts-node
is installed in node_modules
, so it can be used in the scripts section of the package.json
, or called directly,
node_modules/ts-node/dist/bin.js src/index.ts