如何在单独的文件中声明和导入Typescript接口 [英] How to declare and import typescript interfaces in a separate file

查看:214
本文介绍了如何在单独的文件中声明和导入Typescript接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于打字稿的项目中,我想在自己的文件中定义几个接口,从中我将实现用于生产的类以及用于测试的模拟.但是,我不知道什么是正确的语法.我已经找到了很多有关声明接口和实现它们的教程,但是它们都在同一个文件中实现了接口和派生类的微不足道的实现,这不是很真实.导出和导入接口的正确方法是什么?

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 the correct syntax is. I've found plenty of tutorials on declaring interfaces and implementing them, but they all have a trivial implementation of both the interface and derived classes in the same file, which isn't very real-world. What's the right way to export and import the interfaces?

推荐答案

您需要从定义的文件中导出接口,并将其导入到要使用的位置.

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

IfcSampleInterface.ts中:

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

SampleInterface.ts

import { IfcSampleInterface } from './IfcSampleInterface';
let sampleVar: IfcSampleInterface;

这篇关于如何在单独的文件中声明和导入Typescript接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆