【问题标题】:React Native TypeScript Error: Module '"react-native"' has no exported member 'Appearance'React Native TypeScript 错误:模块“react-native”没有导出成员“外观”
【发布时间】:2020-11-29 17:05:32
【问题描述】:

我最近在我的应用程序中实现了暗模式,但是在使用 npm build 构建 npm 包时遇到了错误问题。该代码有效,但有没有办法或理由来消除此错误?

React Native Docs: Appearance

错误

src/ContactActionSheet.tsx:3:10 - error TS2305: Module '"react-native"' has no exported member 'Appearance'.

3 import { Appearance, Dimensions, StyleSheet, Text, View, Linking, TouchableOpacity } from 'react-native';

JSX

// Imports: Dependencies
import { Appearance, Dimensions, StyleSheet, Text, View, Linking, TouchableOpacity } from 'react-native';

// Dark Mode
const colorScheme = Appearance.getColorScheme();

【问题讨论】:

    标签: reactjs typescript react-native


    【解决方案1】:

    如果你使用Vscode发现这个错误信息linting,那是因为你没有添加@types/...包括@types/react-native

    【讨论】:

    • 编辑:我通过启动终端并运行 npm install --save @types/react-native 解决了
    【解决方案2】:

    感谢您跟进回复。在检查了我使用的版本后,我发现我什至没有添加 @types/react-native 模块。一旦我添加了它,我的问题就解决了。奇怪的是,如果你用 TypeScript 开始一个项目,它甚至不能保证有这种依赖关系。

    【讨论】:

    • 这取决于包。一些 NPM 包有内置的类型,但其他 NPM 包有单独的 @types/ 包
    • 以下是一些不同类型包的示例:@testing-library/jest-dom、@types/jest、@types/react-redux、@types/react-router-dom、@types/redux-logger
    【解决方案3】:

    问题在于 npm 包 @types/react-native 需要更新。

    【讨论】:

    • npm install --save @types/react-native
    猜你喜欢
    • 2021-01-29
    • 2021-01-12
    • 2022-01-19
    • 2022-11-21
    • 1970-01-01
    • 2022-01-16
    • 2021-12-19
    • 2021-01-26
    • 1970-01-01
    相关资源
    最近更新 更多