简体   繁体   中英

How to organize TypeScript interfaces in several projects

I want to define several interfaces in their own file in my typescript-based project, from which I'll implement classes for production as well as mocks for testing.

However, I can't figure out what is the best way or practice to implement these interfaces in several projects in different locations. I've found plenty of tutorials on declaring interfaces and implementing them, but they keep both the interface and its implementations in the same project. What's the best practice to export and import these interfaces outside a project like in Java?

You need to export the interface from the file in which is defined and import it wherever you want to use it.

In interfaces.ts :

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

In file.ts :

import { SampleInterface } from './Interfaces';
const sampleVar: SampleInterface;

You can declare more than one interface in .ts file. You must export the interface in order to use them in other files or projects.

Take a look at Module Resolution in the TypeScript documentation

This covers the different strategies for importing modules depending on what type of project you have

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM