【问题标题】:react-native-web typescript error when passing props to svg component将道具传递给svg组件时出现react-native-web typescript错误
【发布时间】:2021-04-05 07:55:22
【问题描述】:

我已将 Metro 和 webpack 配置为像这样导入 svg:

import PlayIcon from '../../assets/icons/play-icon.svg';

...

return () => <PlayIcon />

问题是当我尝试传递道具时,我的编辑器(vscode)和 webpack 终端都出现打字错误:

Type '{ width: number; height: number; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'width' does not exist on type 'IntrinsicAttributes'

这是我的tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "es6"
    ],
    "allowJs": true,
    "jsx": "react",
    "noEmit": true,
    "isolatedModules": true,
    "strict": false,
    "moduleResolution": "node",
    "baseUrl": "./",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ],
  "include": [
    "src",
    "src/types/definitions/svg.d.ts"
  ]
}

src/types/definitions/svg.d.ts:

declare module '*.svg' {
    import { SvgProps } from 'react-native-svg';
    const content: React.FC<SvgProps>;
    export default content;
}

// also tried, amongst others
declare module '*.svg' {
    const content: any;
    export default content;
}

我尝试过的事情:

  1. 将“**/*.svg”传递给 tsconfig.json.exclude
  2. 声明 svg 模块定义的不同方法。我在此处发布的内容是我在许多教程和参考资料中找到的内容。

那我做错了什么?

【问题讨论】:

    标签: reactjs typescript react-native svg react-native-web


    【解决方案1】:

    对我有用的是添加一个文件src/declarations.d.ts

    declare module '*.svg' {
        import React from 'react';
        import { SvgProps } from 'react-native-svg';
        const content: React.FC<SvgProps>;
        export default content;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多