【发布时间】:2020-08-27 20:32:51
【问题描述】:
我有一个名为 ColorBox 的组件,在它的文件夹中我有组件本身、它的样式和一个索引文件以及我的类型的 index.d.ts 文件:
ColorBox
|- Colorbox.tsx
|- index.ts
|- styles.ts
|- index.d.ts
这是我的index.d.ts 文件:
export interface IProps {
colorName: string;
colorHex: string;
}
export interface IBGColor {
backgroundColor: string;
}
这是我的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;
问题是 IProps 和 IBGColor 类型不会自动从 index.d.ts 文件导入,它是如何工作的,我一直在阅读有关 TypeScript 的文章,有人提到如果你放一个 @987654330 @ 模块内的文件,TypeScript 将依赖它并尝试从该文件中查找类型,但为什么它在我的组件中不起作用?
我应该在我的模块中显式导入使用的类型吗?如果是这样,那么index.d.ts文件给我们带来了什么好处?
我也使用常规的 react-native init 项目,而不是任何其他花哨的样板!
【问题讨论】:
标签: javascript reactjs typescript react-native