【发布时间】:2022-06-20 03:56:34
【问题描述】:
我正在使用 TypeScript 创建一个 SVG 图标库。到目前为止,SVGR 非常棒,但我需要的最后一点是生成的类型,以允许将 title 和 ref 传递给捆绑的组件。
复制
我不确定看到哪些内容可能会有所帮助,因此我将包含一个 repo 和一些代码示例。但是通过以下设置,svgr 正在从 SVG 导入创建组件,但没有生成任何类型。结果,每当我尝试使用此包中的图标时,都会警告我找不到它的声明文件。
POC 回购:https://github.com/yuschick/icon-demo
src/index.ts
export { default as TestIcon } from "./svg/test.svg";
export { default as ArrowThinLeft } from './svg/arrow-thin-left.svg';
package.json
{
"module": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"scripts": {
"build": "node esbuild.config.js",
},
"devDependencies": {
"esbuild": "^0.14.39",
"esbuild-plugin-svgr": "^1.0.1",
"typescript": "^4.6.3"
}
}
esbuild.config.js
import esbuild from "esbuild";
import svgr from "esbuild-plugin-svgr";
esbuild.build({
bundle: true,
entryPoints: ["src/index.ts"],
external: ["react"],
format: "esm",
// minify: true,
outfile: "dist/index.js",
plugins: [svgr({ titleProp: true, ref: true, expandProps: false, typescript: true })],
})
.catch(() => process.exit(1));
tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"baseUrl": "src",
"checkJs": true,
"declaration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"outDir": "dist",
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "esnext"
},
"include": ["src", "global.d.ts"]
}
global.d.ts
declare module '*.svg' {
const content: string;
export default content;
}
预期行为
通过将typescript: true 传递给svgr 选项,我期望根据生成的组件生成index.d.tsx 文件。
根据 SVGR 文档:
生成带有TypeScript 类型的 .tsx 文件。
注意:虽然我在 ESBuild 中使用 SVGR,但在尝试使用 Rollup 时也存在同样的问题。
【问题讨论】:
标签: typescript svg types .d.ts esbuild