找不到模块“styled-components/native”的声明文件

Fro*_*Dog 27 typescript react-native styled-components

如果您添加styled-components到 React Native 项目,则会有一个专门用于本机组件的子目录:

import styled from 'styled-components/native`;

export const Container = styled.View`
  ...
`;
Run Code Online (Sandbox Code Playgroud)

如果您尝试在 React Native TypeScript 项目中执行此操作,您将遇到以下打字错误:

Could not find a declaration file for module 'styled-components/native'.
Run Code Online (Sandbox Code Playgroud)

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

Fro*_*Dog 50

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
Run Code Online (Sandbox Code Playgroud)

或者

yarn add -D @types/styled-components-react-native
Run Code Online (Sandbox Code Playgroud)


Miš*_*išo 46

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

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

Run Code Online (Sandbox Code Playgroud)

  • 唯一在这里有效的解决方案!如果即使将这一行添加到``tsconfig.json``'也无法解决,只需重新加载 vscode 窗口(如果使用)。 (3认同)