مرکزی مواد پر جائیں
eLearner.app
ماڈیول 7 · سبق 1 از 2کورس میں 13/14~12 min
ماڈیول اسباق (1/2)

tsconfig.json کے ساتھ کنفیگریشن

The tsconfig.json file specifies the root directory of a TypeScript project and the compiler options required to build the project.

Whenever you run tsc in a project directory, the compiler searches for this file to guide its compilation behavior.


Structure of a tsconfig.json File

A typical configuration file looks like this:

Code
{
  "compilerOptions": {
    "target": "es2022",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "./dist"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

The main properties are:

  • compilerOptions: Settings for the compiler.
  • include: Specifies the files or folders to compile.
  • exclude: Specifies the files or folders to ignore during compilation.

Key Compiler Options

Here are some of the most important settings to know:

  1. target: Specifies the version of JavaScript output by the compiler (e.g., es5, es6/es2015, es2020, esnext).
  2. module: Specifies the module system used in the compiled code (e.g., commonjs, esnext, node16).
  3. strict: Enables a broad suite of rigorous type-checking behaviors. It is highly recommended to set this to true for maximum type safety. It enables options like:
    • noImplicitAny: Raises an error on expressions and declarations with an implied any type when no type annotation is provided.
    • strictNullChecks: Prevents assigning null or undefined to types that do not explicitly include them in their union.

Try it yourself

Exercise 1: Handling strictNullChecks

ورزش#ts.m7.l1.e1
کوششیں: 0لوڈ ہو رہا ہے…

Under the strictNullChecks: true option in tsconfig.json, we cannot assign null or undefined to primitive types. Declare a function named parseAge that accepts an input of type string | null and returns a number if the input is not null and is a valid integer, otherwise it returns 0. Explicitly handle the null case to pass strict compilation.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

Check if input is null at the start of the function and return 0 immediately before processing the string.

3 کوششوں کے بعد حل دستیاب ہے۔

Exercise 2: Strict Typing without Implicit Any

ورزش#ts.m7.l1.e2
کوششیں: 0لوڈ ہو رہا ہے…

Under noImplicitAny: true, any parameter whose type cannot be inferred must be explicitly typed. Create a function named processItems that accepts an array items (array of numbers) and a callback function that takes a single number as an argument and returns nothing. Make sure not to use any and type everything explicitly.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

Declare the function with signature function processItems(items: number[], callback: (item: number) => void): void.

3 کوششوں کے بعد حل دستیاب ہے۔