Hone logo
Hone
Problems

Implementing Ambient Declarations in TypeScript

Ambient declarations in TypeScript allow you to provide type information for existing JavaScript libraries or code that isn't written in TypeScript. This is crucial for leveraging external codebases while still benefiting from TypeScript's type checking and autocompletion features. Your task is to create a TypeScript file that correctly declares the types for a simplified JavaScript library that provides a single function, greet.

Problem Description

You are given a JavaScript library (represented by a string containing the JavaScript code) that defines a function greet(name: string): string. This function takes a name as a string and returns a greeting string. You need to create a TypeScript declaration file (.d.ts) that accurately describes the greet function, enabling TypeScript to understand and type-check code that uses this JavaScript library. The declaration file should allow TypeScript to provide autocompletion and error checking when calling greet.

Key Requirements:

  • Create a .d.ts file.
  • Declare the greet function with the correct parameter type (string) and return type (string).
  • The declaration should be ambient (global scope).
  • The declaration should allow TypeScript to correctly infer the types when using the greet function.

Expected Behavior:

When a TypeScript file imports or uses the JavaScript library (assuming the library is available at a specific path), TypeScript should:

  • Recognize the greet function.
  • Provide autocompletion for the name parameter when calling greet.
  • Infer the return type of greet as string.
  • Generate type errors if the arguments passed to greet are of the wrong type.

Edge Cases to Consider:

  • The JavaScript library might not have any other exports besides greet.
  • The JavaScript library might be used in different parts of a larger project.

Examples

Example 1:

JavaScript Library (library.js):
function greet(name: string): string {
  return "Hello, " + name + "!";
}
// declaration.d.ts
declare function greet(name: string): string;

Explanation: This declaration accurately reflects the signature of the greet function in the JavaScript library. TypeScript will now understand the function's type.

Example 2:

JavaScript Library (library.js):
window.greet = function(name: string): string {
  return "Hello, " + name + "!";
}
// declaration.d.ts
declare function greet(name: string): string;

Explanation: Even though the function is attached to the window object in JavaScript, the TypeScript declaration can still declare it as a global function. TypeScript will understand the function's type.

Constraints

  • The .d.ts file should be as concise as possible while accurately representing the JavaScript library's API.
  • The declaration should be ambient (not part of a module).
  • You are not required to write the JavaScript library itself; only the TypeScript declaration file.
  • Assume the JavaScript library is available at a known path during TypeScript compilation (e.g., import * as lib from './library.js';).

Notes

  • Ambient declarations are useful when you can't or don't want to convert an entire JavaScript library to TypeScript.
  • The declare keyword is essential for ambient declarations. It tells TypeScript that the type exists elsewhere.
  • Consider how the JavaScript library exposes its functions (e.g., global function, attached to an object). This will influence how you declare the type in TypeScript.
  • Focus on accurately representing the function signature (name, parameters, return type).
Loading editor...
typescript