Declaration Files & Modules Questions with Answers

18 practice questions on Declaration Files & Modules (TypeScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

What does export * from "./shapes"; do?

  1. Exports ./shapes as a default object
  2. Re-exports every named export of ./shapes
  3. Copies the file ./shapes into the current one
  4. Imports ./shapes for its side effects only

Answer: B. Re-exports every named export of ./shapes

Explanation

It forwards named exports, which is common in index files that gather a folder's public API. The default export is not included.

Step-by-step solution

  1. Export * from './shapes' re-exports the named exports of that module.
  2. The default export is not included.
  3. So it re-exports every named export of ./shapes (option B).

Why the other options are wrong

  • A. It does not create a default export object.
  • C. The file is not copied; its exports are forwarded.
  • D. Named exports are forwarded, so it is not only for side effects.

Tip: Use export * from in index files to gather a folder's public exports.

Question 2Easy

In a library's package.json, what does the "types" field point to?

  1. The TypeScript compiler version to use
  2. The entry .d.ts file for the package
  3. The tsconfig.json used to build it
  4. The list of @types packages to install

Answer: B. The entry .d.ts file for the package

Explanation

Editors and tsc read it to find the package's type declarations. The field is also spelled typings in older packages.

Step-by-step solution

  1. The "types" (or "typings") field in package.json names the entry declaration file.
  2. TypeScript reads it to find the package typings.
  3. For example "types": "dist/index.d.ts".

Why the other options are wrong

  • A. It does not set a compiler version.
  • C. It does not point to a tsconfig.
  • D. @types packages are installed by npm separately.

Tip: types field = path to .d.ts.

Question 3Easy

What does "declaration": true in tsconfig.json do?

  1. Reads type declarations from .js comments
  2. Requires every variable to have a declared type
  3. Emits a .d.ts file next to each compiled JS file
  4. Allows declare statements to appear inside .ts files

Answer: C. Emits a .d.ts file next to each compiled JS file

Explanation

Libraries enable it so consumers get type information alongside the JavaScript they publish.

Step-by-step solution

  1. Declaration true makes tsc emit a .d.ts file for each compiled file.
  2. The .d.ts files describe the public types of the code.
  3. So consumers get type information next to the JavaScript (option C).

Why the other options are wrong

  • A. Tsc does not read type declarations from .js comments for this option.
  • B. It does not require every variable to have a declared type.
  • D. This option only emits .d.ts files; declare statements work regardless.

Tip: Turn on declaration in libraries so their .d.ts files ship with the JavaScript.

Question 4Easy

How many default exports can one module have?

  1. Any number
  2. One
  3. One per function
  4. None in TypeScript

Answer: B. One

Explanation

export default can appear once per module. A module can have any number of named exports alongside it.

Step-by-step solution

  1. Export default can appear only once in a module.
  2. A module can have any number of named exports alongside that one default.
  3. So the answer is one (option B).

Why the other options are wrong

  • A. A module can have only one default export.
  • C. Functions do not each get a default export; there is one per module.
  • D. TypeScript supports default exports.

Tip: Each module has at most one default export; use named exports for everything else.

Question 5Medium

What does /// <reference types="node" /> at the top of a file do?

  1. Loads a file named node.ts
  2. Imports Node's runtime modules
  3. Includes the @types/node declarations
  4. Marks the file as Node-only at runtime

Answer: C. Includes the @types/node declarations

Explanation

A triple-slash types directive pulls a declaration package into the compilation. It is mostly used in .d.ts files; tsconfig types settings are the usual alternative.

Step-by-step solution

  1. A triple-slash types directive includes type packages.
  2. types="node" pulls @types/node.
  3. It affects types only.

Why the other options are wrong

  • A. It does not load node.ts.
  • B. No runtime import.
  • D. No runtime effect.

Tip: reference types = include typings.

Question 6Medium

How does TypeScript treat a .ts file that has no import or export statements?

  1. As a declaration file, like a .d.ts
  2. As a module whose declarations are all private
  3. As a script whose top-level declarations are global
  4. As an error; every file needs an export

Answer: C. As a script whose top-level declarations are global

Explanation

Only files with a top-level import or export are modules. Adding export {} turns a script into a module.

Step-by-step solution

  1. A file is a module only if it has a top-level import or export.
  2. This file has neither, so it is a script.
  3. In a script, top-level declarations join the global scope.
  4. So TypeScript treats it as a script whose top-level declarations are global (option C).

Why the other options are wrong

  • A. A .ts file is not a declaration file just because it has no imports.
  • B. A module is created only by import or export, and this file has neither.
  • D. No error is raised; scripts are allowed.

Tip: Add export {} to a file with no imports or exports to keep its names out of the global scope.

Question 7Medium

What does this declaration do?

declare global {
  interface Window { appVersion: string }
}
export {};
  1. Creates a global variable called Window
  2. Adds appVersion to the global Window type
  3. Replaces the DOM's Window type entirely
  4. Exports the Window type from the current module

Answer: B. Adds appVersion to the global Window type

Explanation

declare global lets a module file add to global types. The interface merges with the built-in Window declaration.

Step-by-step solution

  1. Declare global lets a module file add declarations to the global scope.
  2. Inside it, interface Window is declared with appVersion as a string.
  3. Because the file is a module (export {}), this merges into the built-in Window interface.
  4. So appVersion is added to the global Window type (option B).

Why the other options are wrong

  • A. It is an interface merge; no global variable is created.
  • C. The interface merges with the built-in Window; nothing is replaced.
  • D. Nothing is exported; the global block is what adds the field.

Tip: Use declare global inside a module file to extend built-in global types such as Window.

Question 8Medium

What does declare module "legacy-lib"; (with no body) do?

  1. Downloads types for the module from npm
  2. Prevents the module from being imported anywhere
  3. Lets you import the module; its imports are typed any
  4. Generates a .d.ts file for the module

Answer: C. Lets you import the module; its imports are typed any

Explanation

A shorthand ambient module declaration silences "cannot find module" errors, at the cost of every import from it being any.

Step-by-step solution

  1. A declare module statement with a name and no body is a shorthand ambient module.
  2. TypeScript then accepts imports from legacy-lib without a cannot find module error.
  3. Every value imported from it is typed any, so nothing from it is checked.
  4. So the answer is option C: imports work and are typed any.

Why the other options are wrong

  • A. No download happens; the statement is a local declaration in your code.
  • B. Importing is allowed; the declaration exists so that imports work.
  • D. No .d.ts file is generated; the declaration only silences the error.

Tip: A shorthand ambient module makes an untyped package importable, but everything from it is any.

Question 9Hard

With "module": "nodenext" and an ES module package, how is a relative import of ./util.ts written in the source?

  1. import { x } from "./util.js";
  2. import { x } from "./util.ts";
  3. import { x } from "util";
  4. import { x } from "./util";

Answer: A. import { x } from "./util.js";

Explanation

Node's ESM loader needs full file extensions, and tsc never rewrites import paths. So the source names the .js file that will exist after compiling.

Step-by-step solution

  1. With nodenext and an ES module package, Node needs the full file name in the import.
  2. Tsc does not rewrite import paths.
  3. So the source names util.js, the file that exists after compiling.
  4. The import is written as ./util.js (option A).

Why the other options are wrong

  • B. The .ts ending is not allowed in this import; the source names the emitted .js file.
  • C. A relative path is needed; a bare name would look up a package called util.
  • D. In nodenext ESM the file extension is required.

Tip: In nodenext ESM, write relative imports with the .js extension even though the source file is .ts.

Question 10Hard

What is the purpose of declare module "*.svg" { const src: string; export default src; }?

  1. Types imports of any .svg file as a string
  2. Forbids importing SVG files in .ts code
  3. Tells tsc to copy SVG files to outDir
  4. Converts SVG files into TypeScript code at build time

Answer: A. Types imports of any .svg file as a string

Explanation

A wildcard module declaration types non-code imports that a bundler handles. TypeScript itself does nothing with the files.

Step-by-step solution

  1. The wildcard module name matches any import whose path ends in .svg.
  2. Each such import is typed by the declaration as a default export of type string.
  3. TypeScript does nothing with the files; the bundler handles them.
  4. So imports of .svg files are typed as string (option A).

Why the other options are wrong

  • B. Importing SVG files is allowed; the declaration types them.
  • C. Tsc does not copy SVG files; that is the bundler's job.
  • D. The declaration does not convert files; it only types them.

Tip: A wildcard module declaration types non-code imports such as images or styles.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all TypeScript topics · All topics