Given the custom declaration file math-utils.d.ts below, what will be the output of the TypeScript code?
declare module 'math-utils' { export function square(x: number): number; } import { square } from 'math-utils'; console.log(square(5));
Think about what a declaration file does and what it does not provide.
The declaration file math-utils.d.ts only tells TypeScript about the shape of the module but does not provide an implementation. Without the actual JavaScript code for 'math-utils', the compiler or runtime will fail to find the module.
Choose the correct statement about writing custom declaration files in TypeScript.
Think about the purpose of declaration files in TypeScript.
Declaration files (.d.ts) describe the types and interfaces of JavaScript code so TypeScript can type-check code that uses those modules, even if the source code is not available.
Consider the following declaration file custom-lib.d.ts and usage code. Why does TypeScript report an error?
declare module 'custom-lib' { export const version: string; export function greet(name: string): void; } import { version, greet } from 'custom-lib'; greet(42);
Check the function parameter types in the declaration and usage.
The declaration specifies greet takes a string, but the code calls it with a number (42), causing a type error.
Choose the correct syntax for declaring a module that exports a class named Person with a constructor taking a name string.
Remember that declaration files only describe types and signatures, not implementations.
Option B correctly declares the class and constructor signature without implementation. Option B misses the type annotation. Option B provides an implementation body, which is not allowed. Option B incorrectly specifies a return type for a constructor.
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;
}
}Think about how TypeScript merges interface declarations with the same name in the same module.
TypeScript merges interface declarations with the same name by combining their properties. Here, Config has host, port, and secure, totaling 3 properties.