Skip to content

Instantly share code, notes, and snippets.

@palashmon
Last active February 27, 2024 05:13
Show Gist options
  • Save palashmon/7484d932ece4c68d9b1c161089149ce1 to your computer and use it in GitHub Desktop.
Save palashmon/7484d932ece4c68d9b1c161089149ce1 to your computer and use it in GitHub Desktop.
Creating and Importing Multiple TypeScript Declaration Files as a Single Index File

Creating and Importing Multiple TypeScript Declaration Files as a Single Index File

To create multiple Child1Props.d.ts files and import them as a single index.d.ts file, you can follow these steps:

  1. Define the Individual Declaration Files:

    • Create each Child1Props.d.ts file as needed, each containing the type definitions specific to that component.
  2. Export Declarations in Each .d.ts File:

    • Ensure that each Child1Props.d.ts file exports its declarations. You can export types, interfaces, functions, or any other declarations you need.
  3. Create the Index Declaration File:

    • In the index.d.ts file, import and re-export all declarations from the individual Child1Props.d.ts files. This consolidates them into a single entry point for importing
  4. Usage in JavaScript Files:

    • In your JavaScript files, you can now import all declarations from index.d.ts as a single entry point.

This structure allows you to organize your type definitions for multiple components into separate files while providing a convenient way to import them all at once through the index.d.ts file.

Remember to adjust the import paths in the index.d.ts file and in your JavaScript files according to your project's directory structure.

export interface Child1Props {
name: string;
languages: Array<string>;
}
// import types can be used in type alias declarations
/**
* @typedef {import("./types").Child1Props} Child1Props
*/
/**
* @type {Child1Props}
*/
const props = {};
props.name;

Distributed Types Directory

For larger projects, consider placing your .d.ts files close to their relevant JavaScript files. This approach makes it easier to manage and update types alongside their corresponding JavaScript code.

/src
  /components
    - Component.js
    /types
      - Child1Props.d.ts
      - Child2Props.d.ts
      - index.d.ts
export * from './Child1Props.d';
export * from './Child2Props.d';
// Add more exports if you have additional components
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment