typescript 无法导出Typescirpt中的接口-请求的模块未提供名为Settings的导出

aiazj4mn  于 7个月前  发布在  TypeScript
关注(0)|答案(3)|浏览(120)

我试图在Typescript中导出/导入接口,但我得到了这个错误,我不知道我在这里做错了什么
错误:请求的模块“/src/types/settings.ts”不提供名为“”的导出“
在我的types/settings.ts中,

export interface Settings {
    activeUser: number
}

字符串
我像这样输入

import { Settings } from '@/types/settings'


下面是我的tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "baseUrl": "./src/",
    "useDefineForClassFields": true,
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "paths": {
      "@/*": ["src/*"],
    },
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"],
    "isolatedModules": false
  },

  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}


我正在使用Vue/Vite与Typescript

s4n0splo

s4n0splo1#

奇怪的是,npm run build可以正常工作,但npm run dev会抛出SyntaxError。
我被迫使用import type { ...,因此也需要从vue相同的触摸Ref
已在GitHub上发布Vite issue 731的解决方法

xnifntxz

xnifntxz2#

paths中的错误是相对于baseUrl解析的。试试这个:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}

字符串
或者这个

{
  "compilerOptions": {
    "baseUrl": "./src/",
    "paths": {
      "@/*": ["./*"]
    }
  }
}

pkmbmrz7

pkmbmrz73#

很可能是导入接口而没有显式地将其标记为类型的问题。Vite要求isolatedModules为true,这反过来又要求以这种方式处理接口:

interface IFoo { bar: string; }
export type { IFoo };

字符串
如果您正在使用类似于以下的桶文件(index.ts),请保持在中间:

export { IFoo } from './foo';


你应该把它们转换成这样:

export * from './foo';


还是这样

import type { IFoo } from "./foo";
export type { IFoo };


另一个肮脏的选择是在tslog.json中将skipLibCheck设置为true,这将降低类型的严格性。

相关问题