React Native 未能找到模块“styled-components/native”的声明文件

xdnvmnnf  于 2023-03-31  发布在  React
关注(0)|答案(3)|浏览(273)

如果你将styled-components添加到你的React Native项目中,有一个子目录专门用于本地组件:

import styled from 'styled-components/native`;

export const Container = styled.View`
  ...
`;

如果你尝试在React Native TypeScript项目中这样做,你会遇到以下类型错误:

Could not find a declaration file for module 'styled-components/native'.

解决此问题的典型方法是在开发依赖项中安装@types/styled-components模块,但这并不能解决问题。

v440hwme

v440hwme1#

https://github.com/styled-components/styled-components/issues/2099#issuecomment-749776524
styled-components/native类型移动到@types/styled-components-react-native
因此,要解决:

npm install --save-dev @types/styled-components-react-native

yarn add -D @types/styled-components-react-native
lb3vh1jj

lb3vh1jj2#

如果有人安装了@types/styled-components-react-native,仍然得到错误,也许它有助于技巧,如https://github.com/styled-components/styled-components/issues/2370所述。对我来说,它有助于将类型添加到tsconfig.json文件:

{
  "extends": "some/tsconfig.base",
  "compilerOptions": {
    // ...
    "types": [
      "jest",
      "cypress",
      "@testing-library/cypress",
      "@types/styled-components-react-native"
    ]
  },
  // ...
}
tyu7yeag

tyu7yeag3#

安装

npm install --save @types/styled-components

相关问题