[英]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?问题是
IProps
和IBGColor
类型不会从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.