Environment Setup and tsconfig
Install TypeScript, run the compiler, understand project configuration, and enable strict checking.
Install and Run TypeScript
For a project, install TypeScript as a development dependency so everyone and CI use the same version. npx tsc --init creates a starting tsconfig.json; npx tsc compiles the project according to that configuration. Avoid relying on an arbitrary globally installed compiler for team projects.
A minimal Node-oriented project often has src/ as input and dist/ as output. The exact module and target settings depend on whether the project uses Node ESM, CommonJS, a browser bundler, or a framework. Follow the runtime's module conventions instead of copying a configuration without understanding it.
npm init -y
npm install --save-dev typescript
npx tsc --init
npx tsc --noEmit- Install TypeScript locally for reproducible builds.
tsconfig.jsoncontrols the compiler's project behavior.- Choose module settings to match your runtime and tooling.
Why is a local TypeScript development dependency usually preferable?
Understand tsconfig.json
tsconfig.json defines the files and compiler behavior for a TypeScript project. Important options include target (JavaScript language level), module and moduleResolution (how imports are emitted and resolved), rootDir and outDir (source/output layout), strict (strict type-checking family), and noEmit (check without output).
Do not treat compiler flags as isolated switches. For example, Node's ESM rules differ from browser bundler resolution. A project should use a configuration aligned with its deployment target and build system. Extend a framework-provided configuration when the framework expects one.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"strict": true,
"noEmitOnError": true
},
"include": ["src/**/*.ts"]
}NodeNext behavior also depends on package metadata and file extensions. Frontend projects may use moduleResolution: "Bundler" or framework defaults.targetcontrols the JavaScript syntax level.moduleandmoduleResolutionmust agree with the environment.strictenables a family of checks;noEmitOnErrorcan prevent output after errors.
Which option enables the strict family of type-checking options?
Strict Mode and noImplicitAny
Strict checking is one of the highest-value choices in a new codebase. strictNullChecks makes null and undefined distinct concerns; noImplicitAny flags values whose types would otherwise silently become any; strictFunctionTypes tightens function parameter compatibility in relevant positions.
Enable strictness early, then fix errors at their source. Do not silence every error with any or as SomeType: doing so removes the evidence the compiler is providing. For legacy migrations, enable checks incrementally and track unsafe areas explicitly.
function getDisplayName(name: string | undefined): string {
if (name === undefined) {
return "Guest";
}
return name.toUpperCase();
}name from string | undefined to string in the remaining branch, making the call safe under strict null checking.- Prefer strict mode for new projects.
- Narrow nullable values rather than hiding the error.
- Use
unknownat uncertain boundaries instead of defaulting toany.
With `strictNullChecks` enabled, what is true of `string | undefined`?