Skip to content

Defining and Exporting Types in Modules

In TypeScript, you can define types within a module and export them for use in other parts of your application. This allows for better type management and reusability across different modules.

Exporting Types

In TypeScript, when you export a type, it's important to use the type keyword to clearly indicate that the export is a type. This helps in distinguishing between value exports and type exports, especially when importing them in other modules.

Consider a module where we define a simple type and export it:

typescript
// types.ts
export type Size = { width: number; height: number };

In this example, we define a `Size` type with two properties: `width` and `height`. This type is exported from the module, making it available for import in other files.

Importing Types

When importing a type in another module, you should also use the type keyword.

For example, to import the Size type defined in the module above:

typescript
// main.ts
import { type Size } from "./types";

const box: Size = {
  width: 100,
  height: 200
};

The latest version of Vite uses recommended TypeScript settings that require the type keyword when importing types. These settings reflect TypeScript's best practices for managing type imports.