← कोर्स पर वापस जाएँ: TypeScript from scratch

tsconfig in real projects and review

वीडियो जल्द आ रहा है

In the first week you created a minimal tsconfig. Now that you know how to write modules and classes, let us review the options that appear in real projects, understand which ones matter and which you can leave alone. A well-configured tsconfig is a small investment that prevents many errors. You will see that most modern projects use variations of a few base configurations, so there is no need to memorize the whole catalog.

Strictness options beyond strict

strict already covers the essentials, but there are additional very valuable options that are not included. noUncheckedIndexedAccess makes reading an array by index, like items[0], return T | undefined, which is more honest because the index might not exist. noImplicitReturns detects functions that return a value in some branches and forget to in others. noUnusedLocals and noUnusedParameters flag dead code. exactOptionalPropertyTypes distinguishes between an absent property and a property whose value is undefined. Turn them on in new projects; in old projects it is wise to introduce them one at a time.

json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "lib": ["ES2023"],
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noFallthroughCasesInSwitch": true,
    "verbatimModuleSyntax": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "dist",
    "rootDir": "src"
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist"]
}

lib, types and the environment

The lib option decides which global APIs the compiler knows. If you do not specify it, it depends on the target. For a Node project you do not need the DOM, so do not include "DOM"; for a browser project you do. The type definitions for Node come with the @types/node package, which you install as a development dependency: without it, TypeScript does not recognize process or the node:fs and node:path modules. npm packages that do not ship their own types usually have a @types/name package in the DefinitelyTyped repository.

typescript
// needs: npm install --save-dev @types/node
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

const items = ['a', 'b'];
const first = items[0]; // string | undefined with noUncheckedIndexedAccess
console.log(first?.toUpperCase());

const file = join(process.cwd(), 'package.json');
const raw: string = readFileSync(file, 'utf8');
console.log(raw.length > 0);

Review and practice

This week you learned classes with modifiers, inheritance and abstraction, modules with import and export, import type, NodeNext resolution with .js extensions, package.json with type module and a professional-level tsconfig. It is the skeleton the final project will start from. Do the exercise to get that skeleton ready.

If your project uses a framework such as Next.js, Vite or Astro, usually the generator itself creates the right tsconfig and you only add strictness options. Do not replace it with one copied from another project without understanding what changes, because options like jsx, moduleResolution Bundler or noEmit depend on the tool. When something fails in the configuration, read the documentation for that option and try changes one at a time. And remember the tsconfig is code: it is reviewed in changes and versioned together with the project.

जो सीखा उसे परखें

प्रश्न 1 / 6

What does private do on a class property?

✎ मेरे नोट्स

केवल इस ब्राउज़र में सहेजा गया। खाते में रखने के लिए साइन इन करें।0/5000 अक्षर

← पिछलाअगला →