简体   繁体   English

如何通过 Typescript 为 Flow 导入 React-Native 类型?

[英]How to import React-Native types via Typescript for Flow?

In my react-native application I have a TextInput component.在我的 react-native 应用程序中,我有一个TextInput组件。 The TextInput reads some types from the following path: TextInput从以下路径读取一些类型:

/Users/karl/Library/Caches/typescript/3.6/node_modules/@types/react-native/index.d.ts

This file has a bunch of types in including:这个文件有很多类型,包括:

export type KeyboardType = 'default' | 'email-address' | 'numeric' | 'phone-pad';

I can access this file by cmd + clicking on a prop I have added, (using vscode) to go to it's definition.我可以通过cmd + clicking我添加的道具(使用 vscode)到 go 来访问这个文件。

What I am wondering though is how I can reference the types in this file, so I can use them in my Flow typing definitions?我想知道的是如何引用此文件中的类型,以便可以在我的流类型定义中使用它们?

I want to be able to do something like:我希望能够做类似的事情:

// pseudocode
import type { KeyboardType } from 'react-native'

How I can go about this?我怎么能 go 关于这个?

Actually, for flow-typed you should directly add the types from component source:实际上,对于flow-typed ,您应该直接从组件源添加类型:

import type {
  KeyboardType,
  ReturnKeyType,
} from 'react-native/Libraries/Components/TextInput/TextInput';

But for typescript first, install @types/react-native and then get all the types from react-native ;但是对于typescript首先,安装@types/react-native然后从react-native获取所有类型;

import { ReturnKeyType, KeyboardType } from 'react-native';

You're doing everything right, except you don't need the word type after import :你做的一切都是对的,除了import之后你不需要单词type

import { Image, StyleSheet, ReturnKeyType, KeyboardType } from "react-native";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM