Sostieni AppuntiFacili con una piccola donazione su PayPal
Dona con PayPalBefore writing any TypeScript, you need three things in your project folder:
package.json), so the project knows its dependencies and scripts.tsconfig.json), so the compiler knows how to check and build your code.All three come from a handful of npm/npx commands. Let’s go through them one at a time.
INFO
npm (Node Package Manager) ships with Node.js. If npm -v fails in your terminal, install Node.js first — everything else in this lesson depends on it.
npm init -y
This creates a package.json file in the current folder. package.json is the manifest of your project: it records the project’s name, version, scripts, and — crucially — its dependencies (which packages your project needs, and which exact versions).
The -y flag skips the interactive questionnaire (project name, version, description, etc.) and accepts all the defaults, which is convenient for a quick learning project. Without -y, npm init asks you a series of questions before generating the same file.
A freshly generated package.json looks roughly like this:
{
"name": "my-project",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
npm install typescript --save-dev
This installs the typescript package as a dev dependency — a dependency you need while developing/building the project, but not something your shipped application code depends on at runtime. Two things get created as a result:
node_modules/: a folder containing the actual installed packages (here, the TypeScript compiler itself, plus its own dependencies). You should never edit this folder by hand, and it’s normally excluded from version control via .gitignore.package-lock.json: a file that records the exact versions of every installed package (including nested dependencies). This guarantees that anyone else who runs npm install on this project gets byte-for-byte the same dependency tree you have — reproducible builds.TIP
--save-dev (or its shorthand -D) is the right choice for TypeScript itself, linters, test runners, and build tools — anything used only while developing. Runtime libraries your app actually imports and ships (like a UI framework) are installed without that flag.
After this step, package.json gains a new section:
"devDependencies": {
"typescript": "^5.x.x"
}
npx tsc --init
npx runs a locally installed package’s executable without needing to install it globally first — here, it runs the tsc (TypeScript Compiler) binary that now lives in node_modules/.bin/. The --init flag tells tsc to generate a starter tsconfig.json file.
tsconfig.json is the configuration file that tells the compiler:
A trimmed, commented example of the options you’ll actually touch early on:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true
}
}
target: which JavaScript version the compiler outputs (e.g. ES2020, ES2022). Older targets are more compatible with old runtimes, but miss out on modern, more concise JS features.module: which module system to emit (commonjs for classic Node.js require, ESNext/NodeNext for modern ES modules with import/export).rootDir: where your TypeScript source files live (commonly ./src).outDir: where the compiled .js output should be written (commonly ./dist), keeping source and build output separate.strict: turns on the full set of strict type-checking rules (no implicit any, strict null checks, and more). Almost always worth enabling — it is the single option most responsible for TypeScript actually catching bugs.WARNING
The generated tsconfig.json contains dozens of options, most of them commented out. Don’t feel like you need to understand every single one on day one — target, module, rootDir, outDir and strict are enough to get productive.
npm init -y # creates package.json
npm install typescript --save-dev # creates package-lock.json and node_modules/
npx tsc --init # creates tsconfig.json
After running these, your folder contains everything needed to start writing and compiling TypeScript files. The next lesson covers how to actually compile and run them.
What does `npm init -y` create?
Why is TypeScript typically installed with `--save-dev`?
What is the purpose of `package-lock.json`?
What command generates the tsconfig.json file?
What does the `strict` compiler option do?
What is the role of `outDir` in tsconfig.json?
Scenario: You are starting a brand-new TypeScript project from an empty folder, with nothing installed yet.
Task:
npm init -y inside it. Inspect the generated package.json.node_modules/ and package-lock.json were created.npx tsc --init and open the generated tsconfig.json. Locate the target, module, strict, rootDir and outDir options (some may be commented out — uncomment and set them).src folder and set rootDir to ./src and outDir to ./dist in tsconfig.json.Learning objective: get comfortable with the standard npm + TypeScript project bootstrap sequence and understand what each generated file/folder is for.
Prenota una lezione