【问题标题】:How to check type in react-native?如何检查 react-native 中的类型?
【发布时间】:2021-04-20 18:22:22
【问题描述】:

我有一个问题,已经解决了,但是让我对模块感到困惑。

我尝试在我的组件中导入 Animated。

import { Animated, Easing } from 'react-native';

然后,我试图找到它的类型(流),我找到了答案。 Flow types for react-native's Animated library(谢谢)。

import type AnimatedValue from 'react-native/Libraries/Animated/src/nodes/AnimatedValue';

问题是。 当我在import { Animated, Easing } from 'react-native';中打开Animated(右键单击,然后我用“Visual code”进入类型定义),为什么它没有进入它的模块,而是进入这个路径? (库/缓存) 我试图在“react-native”模块中找到这个文件(index.d.ts),但我找不到它。为什么?

然后我尝试阅读 index.d.ts,找出 Animated.Value 的类型。

export namespace Animated {
    type AnimatedValue = Value;
    type AnimatedValueXY = ValueXY;
export class Value extends AnimatedWithChildren {

Horraay,我得到了 Animated.Value 是一个类,它的类型是一个 AnimatedValue。然后像往常一样找到它的路径,我右键单击,然后我用“可视代码”去类型定义,但我什么也没找到。

那么,import type AnimatedValue from 'react-native/Libraries/Animated/src/nodes/AnimatedValue 与 index.d.ts 中的 type AnimatedValue = Value; 有什么关系?

"react-native": "0.63.2",

【问题讨论】:

    标签: reactjs typescript react-native flowtype


    【解决方案1】:

    index.d.ts 文件用于提供有关已用 JavaScript 编写的模块的 typescript 类型信息。这将允许您使用 javascript 模块,而无需先将它们完全转换为 ts,而不会在您的代码上出现任何类型错误。

    为什么它不去它的模块,而是去这个路径?

    这是因为当您使用 npm 为特定库安装类型时,它们会作为 index.d.ts 文件安装在 node_modules/@types/<library-name>/ 位置下。因此,使用 VSCode 的导航到类型定义功能会将您带到这个 index.d.ts 文件,而不是将您带到库中。

    我试图在“react-native”模块中找到这个文件(index.d.ts), 但我找不到它。为什么?

    所有index.d.ts 文件都存储在node_modules/@types/<library-name> 而不是node_modules/<library-name>。因此,您无法在相应的模块中找到这些文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2017-11-01
      相关资源
      最近更新 更多