Categories
TypeScript Answers

How to declare and import TypeScript interfaces in a separate file?

Spread the love

Sometimes, we want to declare and import TypeScript interfaces in a separate file.

In this article, we’ll look at how to declare and import TypeScript interfaces in a separate file.

How to declare and import TypeScript interfaces in a separate file?

To declare and import TypeScript interfaces in a separate file, we can use the export keyword to export interfaces.

For instance, we write

export interface ISampleInterface {
  key: string;
  value: string;
}

to export the ISampleInterface interface with export.

Then we can import it by writing

import { ISampleInterface } from "./ISampleInterface";
let sampleVar: ISampleInterface;

to import the ISampleInterface member from ./ISampleInterface.

And then we use that as a type for the sampleVar variable.

Conclusion

To declare and import TypeScript interfaces in a separate file, we can use the export keyword to export interfaces.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *