首页 > 解决方案 > 如何从同一个文件中导入两个同名的导出

问题描述

我有一个如下所示的 Typescript 文件:

export interface Prisma {
    // Members
}

export const Prisma = (): Prisma => {
    // returns a object with of type Prisma
};

鉴于这两个实体在同一个文件中具有相同的名称(我无法更改),我如何将接口导入另一个文件?写作

import Prisma from './myFile';

总是进口出口const,从不出口interface

标签: typescript

解决方案


基本上打字稿会Prisma根据您使用它的位置来推断您,例如:

// Prisma.ts
export interface Prisma {
  value: string;
}

export const Prisma = (): Prisma => {
  return { value: "Some value" };
};

// File.ts
import { Prisma } from '.Prisma';

class MyClass implements Prisma {
  value: string = "Initial value"; // => implement *interface*
  // ...

  getPrismaValue() {
    return Prisma().value; // => execute Prisma *function*, yields "Some value"
  }
}

推荐阅读