[英]How to manually import a type from an index.d.ts file, and tell VS Code intellisense that a variable is of that type?
[英]Component fails to auto import types from index.d.ts file in React Native
我有一个名为 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 的文章,有人提到如果将index.d.ts
文件放入其中的模块中,TypeScript 将依赖它并尝试从该文件中查找类型,但为什么它在我的组件中不起作用?
我应该在我的模块中显式导入使用过的类型吗? 如果是这样,那么index.d.ts
文件为我们提供了什么好处?
此外,我使用常规的 react-native init 项目,而不是任何其他花哨的样板!
index.d.ts
用于声明全局类型,因为这些类型在你的整个项目中都是可用的,所以你不需要在你的组件中导入它,或者在index.d.ts
文件中导出它,只需使用它.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.