简体   繁体   English

组件无法从 React Native 中的 index.d.ts 文件自动导入类型

[英]Component fails to auto import types from index.d.ts file in React Native

I have a component called ColorBox, and inside of its folder I have the component itself, style of it and an index file and the index.d.ts file for my types:我有一个名为 ColorBox 的组件,在它的文件夹中,我有组件本身、它的样式和一个索引文件以及我的类型的index.d.ts文件:

  ColorBox
   |- Colorbox.tsx
   |- index.ts
   |- styles.ts
   |- index.d.ts

Here is my index.d.ts file:这是我的index.d.ts文件:

export interface IProps {
  colorName: string;
  colorHex: string;
}

export interface IBGColor {
  backgroundColor: string;
}

and this is my ColorBox.tsx component:这是我的ColorBox.tsx组件:

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

import styles from './styles';

const ColorBox: React.FC<IProps> = ({ colorName, colorHex }) => {
  const boxColor: IBGColor = {
    backgroundColor: colorHex,
  };

  return (
    <View style={[styles.box, boxColor]}>
      <Text style={styles.boxText}>{colorName}</Text>
    </View>
  );
};

export default ColorBox;

The problem is that the IProps and IBGColor types does not auto imported from index.d.ts file, how does it works, I've been reading articles about TypeScript and it was mentioned that if you put an index.d.ts file inside of of the module, TypeScript will rely on that and try to find types from that file, but why it does not work in my component?问题是IPropsIBGColor类型不会从index.d.ts文件自动导入,它是如何工作的,我一直在阅读有关 TypeScript 的文章,有人提到如果将index.d.ts文件放入其中的模块中,TypeScript 将依赖它并尝试从该文件中查找类型,但为什么它在我的组件中不起作用?

Should I explicitly import used types inside of my module?我应该在我的模块中显式导入使用过的类型吗? If so, then what is the benefit that index.d.ts file provides to us?如果是这样,那么index.d.ts文件为我们提供了什么好处?

Also I use regular react-native init project, not any other fancy boilerplates!此外,我使用常规的 react-native init 项目,而不是任何其他花哨的样板!

index.d.ts用于声明全局类型,因为这些类型在你的整个项目中都是可用的,所以你不需要在你的组件中导入它,或者在index.d.ts文件中导出它,只需使用它.

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

相关问题 如何从 index.d.ts 文件手动导入一个类型,并告诉 VS Code 智能感知一个变量是那种类型? - How to manually import a type from an index.d.ts file, and tell VS Code intellisense that a variable is of that type? index.d.ts 文件未发布到 NPM - index.d.ts file not published to NPM 如何在index.d.ts typescript定义文件中引用Redux类型? - How to reference Redux types in index.d.ts typescript definition file? 从typescript模块自动生成index.d.ts,类型定义 - Auto generate index.d.ts, type definitions, from a typescript module 如何拆分大型的typescript index.d.ts文件 - How to split large typescript index.d.ts file 是否为canonical-json提供index.d.ts文件? - Providing an index.d.ts file for canonical-json? 在源代码中使用 `index.d.ts` 声明文件 - Consume `index.d.ts` declaration file in source code 为 read-more-react 创建 index.d.ts npm package - create index.d.ts for read-more-react npm package 有没有办法编写一个定义标准js对象类型的index.d.ts声明文件类型? - Is there a way to write an index.d.ts declaration file type that defines a type of a standard js object? 包含`declare module 'graphql/error/GraphQLError' 的声明(.d.ts)文件; 在 angular 项目的 index.d.ts 中 - declaration (.d.ts) file containing `declare module 'graphql/error/GraphQLError'; in index.d.ts in an angular project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM