typescript - TypeScript - What does importing and exporting interfaces as type do?
问题描述
The TS docs mention interfaces and type aliases, but I can't seem to find much info on importing or exporting interfaces as type
and was hoping to get some clarification:
For example an interface can be exported as:
// located in ./MyInterface.ts
export interface MyInterface
{
foo: string;
bar: () => number;
}
export type {MyInterface as MyInterfaceType}
and it can be imported as:
import {MyInterface} from "./MyInterface"
or
import type {MyInterface} from "./MyInterface"
or
import {MyInterfaceType} from "./MyInterface"
or
import type {MyInterfaceType} from "./MyInterface"
Can anyone explain the difference in behavior between each interface import?
解决方案
TypeScript 3.8 为仅类型导入和导出添加了新语法
export type { MyInterface as MyInterfaceType }
仅导出MyInterface
带别名的类型MyInterfaceType
。
export type
仅提供可用于类型上下文的导出,并且也从 TypeScript 的输出中删除。
和
export interface MyInterface
{
foo: string;
bar: () => number;
}
这是一个名为 export 的 ECMAScript 2015 模块。
从 ECMAScript 2015 开始,JavaScript 有了模块的概念。TypeScript 共享这个概念。
任何声明(例如变量、函数、类、类型别名或接口)都可以通过添加 export 关键字来导出。
请参阅导出声明
推荐阅读
- swift - 创建一个可复制的协议默认初始化器
- apache - FallbackResource 不会重写 php 文件
- css - SVG 动画逐步旋转
- r - 使用 for 循环时无法汇总最小值和最大值
- javascript - mousePressed 在 JavaScript 中的构造函数内部 - p5.js
- asp.net-core-2.0 - 将参数从中间件传递到控制器 - asp.net core
- php - Vscode php调试器Illuminate\Contracts\Filesystem\FileNotFoundException:文件在路径中不存在
- angular - 如何在不替换Angular 5中的前一个组件的情况下一个接一个地加载组件
- php - MySQL:发送 QUERY 数据包时出错。PID=2
- javascript - 默认参数导致无法读取未定义的属性?