关于import 和 import type 的问题

来源:8-8 接口数据类型的冗余定义问题

松树下的熊猫

2023-06-22

之前用Vue3,我把 interface接口 全写入一个 interFace.ts 文件, 然后 export 导出
例:

interface Animal{
name:string
age:number
eat:()=>void
}
export interface Dog extend Animal {
bark:()=>void
}

export interface Human extend Animal{
say:()=>void
}

之后在vue文件中引入的时候使用import出现了报错,之后用import type解决,
例:
import {Dog,Human} from ‘…/lib/interFace.ts’ //报错
import type{Dog,Human} from ‘…/lib/interFace.ts’ //正常

但是不太明白,到底什么情况下需要使用import type 还有export type呢?百度给的解答不太接地气,所以希望老师能讲解一下,谢谢。

写回答

1回答

Dell

2023-06-23

如果你只是导出一个类型,而且外部使用导出内容的时候,也只当一个类型使用。那么就export type 和 import type

0
1
松树下的熊猫
谢谢老师解答。
2023-06-25
共1条回复

专为小白设计的TypeScript入门课

Dell老师专为TypeScript小白打造的,全栈式教学TS入门课程

2249 学习 · 506 问题

查看课程