我正在尝试.svg
使用 TypeScript将文件作为 React 组件导入。
根据 React 文档,这是这样做的:
import { ReactComponent as Icon } from './Icon.svg';
按照 TypeScript 文档,我添加了以下内容:
// custom.ts.d
declare module '*.svg' {
const content: any;
export default content;
}
和
// tsconfig.json
{
...
"files": [
"custom.d.ts"
]
}
SVG 正在渲染。但是我收到了一个 TypeScript 错误:
[ts] Module '"*.svg"' has no exported member 'ReactComponent'. [2305]
tsconfig
如果有帮助,这是我的完整文件:
{
"compileOnSave": false,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"jsx": "react",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"skipLibCheck": true,
"esModuleInterop": true,
"baseUrl": ".",
"plugins": [
{
"name": "typescript-styled-plugin"
}
],
"typeRoots": ["./node_modules/@types"],
"lib": ["dom", "es2015", "es2017"]
},
"exclude": ["node_modules", "dist"],
"files": [
"custom.d.ts"
]
}
谢谢!