Bird
Raised Fist0
Typescriptprogramming~20 mins

Writing custom declaration files in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Declaration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code using a custom declaration?

Given the custom declaration file math-utils.d.ts below, what will be the output of the TypeScript code?

Typescript
declare module 'math-utils' {
  export function square(x: number): number;
}

import { square } from 'math-utils';

console.log(square(5));
A25
Bundefined
CTypeError at runtime
DCompilation error: Cannot find module 'math-utils'
Attempts:
2 left
💡 Hint

Think about what a declaration file does and what it does not provide.

🧠 Conceptual
intermediate
1:30remaining
Which statement about custom declaration files is true?

Choose the correct statement about writing custom declaration files in TypeScript.

ADeclaration files provide runtime implementations for modules.
BDeclaration files must always be named with a <code>.ts</code> extension.
CDeclaration files allow TypeScript to understand types of JavaScript modules without source code.
DDeclaration files automatically generate JavaScript code during compilation.
Attempts:
2 left
💡 Hint

Think about the purpose of declaration files in TypeScript.

🔧 Debug
advanced
2:00remaining
Why does this custom declaration cause a TypeScript error?

Consider the following declaration file custom-lib.d.ts and usage code. Why does TypeScript report an error?

Typescript
declare module 'custom-lib' {
  export const version: string;
  export function greet(name: string): void;
}

import { version, greet } from 'custom-lib';

greet(42);
ABecause <code>greet</code> is called with a number instead of a string.
BBecause <code>version</code> is not used in the code.
CBecause the module declaration is missing a default export.
DBecause the declaration file must use <code>export default</code> syntax.
Attempts:
2 left
💡 Hint

Check the function parameter types in the declaration and usage.

📝 Syntax
advanced
2:00remaining
Which custom declaration file syntax is correct for exporting a class?

Choose the correct syntax for declaring a module that exports a class named Person with a constructor taking a name string.

A
declare module 'people' {
  export class Person {
    constructor(name);
  }
}
B
declare module 'people' {
  export class Person {
    constructor(name: string);
  }
}
C
declare module 'people' {
  export class Person {
    constructor(name: string) {}
  }
}
D
declare module 'people' {
  export class Person {
    constructor(name: string): void;
  }
}
Attempts:
2 left
💡 Hint

Remember that declaration files only describe types and signatures, not implementations.

🚀 Application
expert
2:30remaining
How many properties does the declared interface have after merging?

Given these two declaration files merged by TypeScript, how many properties does the interface Config have?

declare module 'app' {
  interface Config {
    host: string;
    port: number;
  }
}

declare module 'app' {
  interface Config {
    secure: boolean;
  }
}
A3
B2
C1
D4
Attempts:
2 left
💡 Hint

Think about how TypeScript merges interface declarations with the same name in the same module.

Practice

(1/5)
1. What is the main purpose of writing a custom declaration file in TypeScript?
easy
A. To tell TypeScript about types in JavaScript or untyped libraries
B. To write JavaScript code that runs faster
C. To convert TypeScript code into JavaScript
D. To create new JavaScript functions

Solution

  1. Step 1: Understand the role of declaration files

    Declaration files provide type information about JavaScript code or libraries without types.
  2. Step 2: Identify the purpose in TypeScript

    This helps TypeScript check code and improve editor support by knowing types.
  3. Final Answer:

    To tell TypeScript about types in JavaScript or untyped libraries -> Option A
  4. Quick Check:

    Custom declaration files = type info for JS [OK]
Hint: Declaration files describe types for untyped JS code [OK]
Common Mistakes:
  • Thinking declaration files speed up code execution
  • Confusing declaration files with code transpilation
  • Believing declaration files create new functions
2. Which syntax correctly declares a module named 'my-lib' in a custom declaration file?
easy
A. module declare 'my-lib' { function foo(): void; }
B. declare module 'my-lib' { export function foo(): void; }
C. declare 'my-lib' module { export function foo(): void; }
D. module 'my-lib' declare { export function foo(): void; }

Solution

  1. Step 1: Recall correct module declaration syntax

    TypeScript uses declare module 'name' { ... } to declare modules.
  2. Step 2: Check each option's syntax

    Only declare module 'my-lib' { export function foo(): void; } uses the correct order and keywords.
  3. Final Answer:

    declare module 'my-lib' { export function foo(): void; } -> Option B
  4. Quick Check:

    Correct module syntax = declare module 'name' [OK]
Hint: Use 'declare module' followed by module name in quotes [OK]
Common Mistakes:
  • Swapping 'declare' and 'module' keywords
  • Omitting quotes around module name
  • Using incorrect keyword order
3. Given this declaration file snippet:
declare module 'utils' {
  export function greet(name: string): string;
}

What is the correct TypeScript output for greet('Alice') if implemented as return `Hello, ${name}!`;?
medium
A. "Hello, Alice!"
B. Error: greet is not defined
C. greet(Alice)
D. Hello, Alice!

Solution

  1. Step 1: Understand the declaration and implementation

    The declaration says greet takes a string and returns a string. The implementation returns a template string greeting.
  2. Step 2: Evaluate the output of greet('Alice')

    Calling greet('Alice') returns the string Hello, Alice! without quotes in the output.
  3. Final Answer:

    Hello, Alice! -> Option D
  4. Quick Check:

    Function returns greeting string = Hello, Alice! [OK]
Hint: String return does not include quotes in output display [OK]
Common Mistakes:
  • Including quotes around string output incorrectly
  • Confusing function call with output text
  • Assuming runtime error without implementation
4. Identify the error in this custom declaration file snippet:
declare module 'math-lib' {
  export function add(a: number, b: number): number
}
medium
A. Missing semicolon after function declaration
B. Wrong module name syntax
C. Function parameters must be optional
D. Cannot export functions in declaration files

Solution

  1. Step 1: Check syntax inside module declaration

    Each statement inside declaration files must end with a semicolon.
  2. Step 2: Identify missing semicolon

    The function declaration line lacks a semicolon at the end.
  3. Final Answer:

    Missing semicolon after function declaration -> Option A
  4. Quick Check:

    Declaration statements need semicolons [OK]
Hint: Always end declarations with semicolons [OK]
Common Mistakes:
  • Forgetting semicolons in declaration files
  • Thinking exports are not allowed
  • Misnaming modules with wrong quotes
5. You want to add a global type for a new property window.myAppVersion in a TypeScript project. Which custom declaration file snippet correctly adds this global property?
hard
A. declare module 'global' { interface Window { myAppVersion: string; } }
B. declare module 'window' { export const myAppVersion: string; }
C. declare global { interface Window { myAppVersion: string; } } export {};
D. interface Window { myAppVersion: string; }

Solution

  1. Step 1: Understand how to add global types

    Use declare global block to augment global interfaces like Window.
  2. Step 2: Check each option for correct syntax

    declare global { interface Window { myAppVersion: string; } } export {}; correctly declares global and extends Window interface, then exports empty to make it a module.
  3. Final Answer:

    declare global { interface Window { myAppVersion: string; } } export {}; -> Option C
  4. Quick Check:

    Global augmentation uses declare global + interface extension [OK]
Hint: Use 'declare global' to add global types safely [OK]
Common Mistakes:
  • Trying to declare global types inside a module
  • Omitting export {} to make file a module
  • Declaring interface without 'declare global'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard