0
0
Typescriptprogramming~20 mins

Declaration file syntax (.d.ts) in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Declaration File 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 declaration usage?
Given the following declaration file and usage, what will be the output when running the compiled JavaScript?
Typescript
declare module "mathUtils" {
  export function add(a: number, b: number): number;
}

import { add } from "mathUtils";
console.log(add(3, 4));
Aundefined
B7
CTypeError: add is not a function
DCompilation error
Attempts:
2 left
💡 Hint
Declaration files only describe types and do not provide implementations.
🧠 Conceptual
intermediate
1:30remaining
Which syntax correctly declares a constant in a .d.ts file?
Select the correct way to declare a constant named PI with type number in a declaration file.
Adeclare const PI: number;
Bconst PI: number = 3.14;
Cdeclare let PI: number = 3.14;
Dexport const PI = 3.14;
Attempts:
2 left
💡 Hint
Declaration files only declare types and do not assign values.
🔧 Debug
advanced
2:00remaining
Identify the syntax error in this .d.ts interface declaration
Find the syntax error in the following declaration file snippet:
Typescript
interface User {
  name: string;
  age: number;
  greet(): void
}
AMissing semicolon after 'name: string'
BInterface keyword should be 'declare interface'
CMethods cannot be declared without implementation in .d.ts files
DProperty 'age' should be optional
Attempts:
2 left
💡 Hint
Check punctuation carefully in interface declarations.
🚀 Application
advanced
2:30remaining
How to declare a function with overloads in a .d.ts file?
You want to declare a function format that can be called with either one string argument or two string arguments. Which declaration is correct in a .d.ts file?
Adeclare function format(text: string | (text: string, style: string)): string;
B
declare function format(text: string): string;
declare function format(text: string, style: string): string;
Cdeclare function format(text: string, style?: string): string;
Ddeclare function format(...args: string[]): string;
Attempts:
2 left
💡 Hint
Overloads are declared as multiple function declarations with the same name.
Predict Output
expert
3:00remaining
What is the type of 'data' after importing from this declaration file?
Given this declaration file snippet and usage, what is the type of data in the TypeScript code?
Typescript
declare module "dataModule" {
  interface Data {
    id: number;
    value: string;
  }
  const data: Data[];
  export default data;
}

import data from "dataModule";

// What is the type of 'data' here?
AObject with properties 'id' and 'value' but not an array
BAny
CUnknown
DArray of objects with properties 'id' (number) and 'value' (string)
Attempts:
2 left
💡 Hint
Look at the declared type of 'data' in the module.