当提供“--isolatedModules”标志时重新导出类型需要使用“export type”.ts

Shi*_*il 39 typescript

我创建了多个接口,并希望从一个通用文件中发送它们,index.ts如下所示:

--pages
------index.interface.ts
--index.ts
Run Code Online (Sandbox Code Playgroud)

现在index.ts我正在导出类似的内容:

export { timeSlots } from './pages/index.interface';
Run Code Online (Sandbox Code Playgroud)

而我的index.interface.ts样子是这样的:

export interface timeSlots {
  shivam: string;
  daniel: string;
  jonathan: string;
}
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试这样做时,它告诉我:

Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.ts(1205)

不确定为什么会出现此错误,有人可以帮忙吗?

jse*_*ksn 72

您只需要在重新导出类型时使用此语法:

export type { timeSlots } from './pages/index.interface';
//     ^^^^
// Use the "type" keyword
Run Code Online (Sandbox Code Playgroud)

或者,如果使用 TypeScript >= 的版本,您可以在每个导出的类型标识符之前4.5使用type修饰符:

export { type timeSlots } from './pages/index.interface';
//       ^^^^
// Use the "type" keyword
Run Code Online (Sandbox Code Playgroud)

第二种方法允许您在单个语句中混合类型export标识符:

export { greet, type GreetOptions } from './greeting-module';
Run Code Online (Sandbox Code Playgroud)

哪里greeting-module.ts可能看起来像这样:

export type GreetOptions = {
  name: string;
};

export function greet(options: GreetOptions): void {
  console.log(`Hello ${options.name}!`);
}
Run Code Online (Sandbox Code Playgroud)