Hone logo
Hone
Problems

Defining Custom Types with TypeScript Declaration Files

TypeScript's strength lies in its type system, allowing for robust and maintainable code. Declaration files (.d.ts) are crucial for describing the shape of JavaScript code (often from libraries or external sources) to the TypeScript compiler, enabling type checking and autocompletion. This challenge focuses on creating these declaration files to define custom types and interfaces for JavaScript objects.

Problem Description

You are tasked with creating TypeScript declaration files (.d.ts) to describe the structure of JavaScript objects. You will be given a JavaScript object structure and must define corresponding TypeScript types and interfaces to accurately represent it. This is essential for leveraging TypeScript's type safety when working with JavaScript libraries or when refactoring JavaScript code to TypeScript.

Specifically, you need to:

  1. Analyze the JavaScript object structure: Understand the properties, their types (string, number, boolean, array, object, etc.), and any nested structures.
  2. Define TypeScript types: Create appropriate TypeScript types (using type) for simple data structures.
  3. Define TypeScript interfaces: Create TypeScript interfaces (using interface) for more complex object structures, including nested objects and arrays of objects.
  4. Export the types/interfaces: Ensure the defined types and interfaces are exported so they can be used in other TypeScript files.

Expected Behavior:

Your declaration files should accurately reflect the structure of the provided JavaScript objects. When imported into a TypeScript file, the compiler should be able to correctly infer the types of variables and expressions based on these declarations, and provide accurate autocompletion and error checking.

Edge Cases to Consider:

  • Optional Properties: JavaScript objects can have optional properties. Represent these using the ? modifier in your TypeScript interfaces.
  • Union Types: Properties can have multiple possible types. Use union types (e.g., string | number) to represent this.
  • Arrays: Arrays of primitive types or arrays of objects need to be correctly defined.
  • Nested Objects: Complex objects with nested properties require careful definition of interfaces and types.
  • Readonly Properties: If a property should not be modified after initialization, use the readonly modifier.

Examples

Example 1:

JavaScript Object:
{
  name: "John Doe",
  age: 30,
  isStudent: false
}
// my-types.d.ts
export interface Person {
  name: string;
  age: number;
  isStudent: boolean;
}

Explanation: The JavaScript object has three properties: name (string), age (number), and isStudent (boolean). The Person interface accurately reflects this structure.

Example 2:

JavaScript Object:
{
  productName: "Laptop",
  price: 1200.50,
  features: ["Fast Processor", "Large Storage", "Long Battery Life"]
}
// product-types.d.ts
export interface Product {
  productName: string;
  price: number;
  features: string[];
}

Explanation: The JavaScript object represents a product with a name (string), price (number), and an array of features (strings). The Product interface correctly defines these types.

Example 3:

JavaScript Object:
{
  id: 123,
  details: {
    firstName: "Alice",
    lastName: "Smith",
    email: "alice.smith@example.com"
  },
  orders: [
    { orderId: "A1", total: 50 },
    { orderId: "A2", total: 100 }
  ]
}
// customer-types.d.ts
export interface Order {
  orderId: string;
  total: number;
}

export interface CustomerDetails {
  firstName: string;
  lastName: string;
  email: string;
}

export interface Customer {
  id: number;
  details: CustomerDetails;
  orders: Order[];
}

Explanation: This example demonstrates nested objects and arrays. We define interfaces for Order, CustomerDetails, and then Customer to accurately represent the structure.

Constraints

  • File Extension: All solution files must have the .d.ts extension.
  • Valid TypeScript: The generated .d.ts files must be valid TypeScript code and compile without errors.
  • Accuracy: The types and interfaces must accurately reflect the structure of the provided JavaScript objects.
  • No Implementation: You are only creating type definitions; no JavaScript implementation is required.
  • Export Statements: All defined types and interfaces must be exported using export.

Notes

  • Focus on defining the shape of the JavaScript objects, not their behavior.
  • Consider using type for simple aliases and interface for more complex object structures.
  • Think about optional properties and how to represent them using the ? modifier.
  • Pay close attention to nested objects and arrays when defining your types and interfaces.
  • Use descriptive names for your types and interfaces to improve code readability.
  • This challenge is about understanding and applying TypeScript's type system to describe existing JavaScript code.
Loading editor...
typescript