为何Rslib无法识别DefinitelyTyped包中的@types/firefox-webext-browser类型?
解决Rslib无法识别
browser命名空间的问题 1. 显式指定要加载的类型包
你的tsconfig.json没有声明需要加载@types/firefox-webext-browser这个全局类型包,TypeScript默认不会自动引入所有@types下的全局类型。修改tsconfig.json,在compilerOptions中添加types字段:
{ "compilerOptions": { "lib": ["DOM", "ES2022"], "jsx": "react-jsx", "module": "es2022", "skipLibCheck": true, "isolatedModules": true, "resolveJsonModule": true, "moduleResolution": "bundler", "useDefineForClassFields": true, "rootDir": "./src", // 添加这一行 "types": ["firefox-webext-browser"] }, "include": ["src/**/*"] }
2. 强制同步Rslib的TypeScript配置
如果添加tsconfig配置后问题依旧,可以在rslib.config.ts中显式传递类型配置,避免Rslib或SWC的默认配置干扰:
import { pluginReact } from '@rsbuild/plugin-react'; import { defineConfig } from '@rslib/core'; export default defineConfig({ plugins: [pluginReact()], source: { tsconfigPath: "./tsconfig.json", entry: { index: ['./src/**'], }, // 显式指定类型包,确保编译时识别 tsCompilerOptions: { types: ["firefox-webext-browser"] } }, lib: [ { bundle: false, dts: true, format: 'esm' }, ], output: { cleanDistPath: true, distPath: "./build/", target: 'web', }, });
3. 确认类型包安装无误
先检查是否已经正确安装了依赖包:
npm install @types/firefox-webext-browser --save-dev
关于SWC覆盖配置的疑问
SWC不会默认覆盖tsconfig.json的types配置,但Rslib在生成类型声明文件(dts: true)时,需要明确声明全局类型依赖才能正确识别browser命名空间。上述步骤可以解决这类问题。
内容的提问来源于stack exchange,提问作者Sonny D
相关产品推荐
相关产品推荐

