Configuring and Understanding tsconfig.json Quiz Quiz

Assess your understanding of tsconfig.json with questions covering its structure, compiler options, and common configuration scenarios. Perfect for those looking to enhance skills in TypeScript project setup and customization.

  1. Purpose of 'compilerOptions' in tsconfig.json

    In a tsconfig.json file, what is the primary purpose of the 'compilerOptions' property?

    1. It sets the output directory for documentation files.
    2. It defines which TypeScript files to include in compilation.
    3. It specifies the rules for the linter.
    4. It controls how the TypeScript compiler behaves during the build.

    Explanation: The 'compilerOptions' property is used to define various settings that determine how the TypeScript compiler processes and compiles your code, such as the target JavaScript version or module system. Option A is incorrect because linter rules are not specified here. Option B refers to file inclusion, which is configured by 'include' or 'files'. Option D mistakenly identifies documentation settings, which are not managed directly by 'compilerOptions'.

  2. Excluding Files Using tsconfig.json

    If you want to prevent the TypeScript compiler from including files in a 'temp' directory, which property should you use in tsconfig.json?

    1. exports
    2. exclude
    3. expunge
    4. extents

    Explanation: The 'exclude' property allows developers to clearly specify patterns or directories that should be skipped by the compiler, such as 'temp'. 'exports' is not a valid tsconfig.json property for this purpose. 'extents' is a misspelling and does not exist, while 'expunge' is not a recognized property. Only 'exclude' achieves the intended behavior.

  3. Inheritance in tsconfig.json

    Suppose you want to share base settings among multiple tsconfig.json files; which property helps with this by referencing a base configuration?

    1. explore
    2. expand
    3. extract
    4. extends

    Explanation: The 'extends' property lets a tsconfig.json file inherit configuration from another, helping maintain consistency across multiple projects. 'expand', 'explore', and 'extract' are similar-sounding, but none are actual tsconfig.json properties relevant to configuration sharing. Only 'extends' provides this form of inheritance.

  4. Meaning of 'outDir' Option

    What is the effect of setting the 'outDir' option in the 'compilerOptions' of tsconfig.json?

    1. It specifies where compiled JavaScript files will be placed.
    2. It outputs type declarations to a specific folder.
    3. It enforces strict type checking rules.
    4. It determines which files to ignore during compilation.

    Explanation: 'outDir' sets the directory where the generated JavaScript files are output after compilation. Option A may seem plausible, but 'outDir' does not specifically target declaration files. Option C refers to exclusion, handled by 'exclude'. Option D describes type checking, which is not controlled by 'outDir' but by options like 'strict'.

  5. Behavior Without 'include' or 'files'

    If you omit both 'include' and 'files' in your tsconfig.json, which files does the TypeScript compiler attempt to compile by default?

    1. All TypeScript (*.ts) files in the directory and subdirectories except those in 'node_modules' or excluded
    2. Only files explicitly listed in a custom script
    3. No files are compiled
    4. Only files in the current directory

    Explanation: By default, the compiler searches for all .ts files in the current directory and its subdirectories, ignoring certain folders like 'node_modules' and anything listed in 'exclude'. The first option is too restrictive, only covering the top-level directory. Option C is incorrect as the compiler defaults to a broad search rather than none. Option D requires explicit file listing, which is not the default behavior.