0
0
Typescriptprogramming~20 mins

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

Choose your learning style9 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.