javascript - 默认导出的 Typescript 接口
问题描述
对于这样的配置文件,我有一个非常基本的文件夹结构:
/config
/button
/colors
/index
代码如下所示:
颜色.ts
export interface Colors {
[key: string]: string,
}
export default {
black: '#111',
grey: '#999',
green: '#4c8857',
red: '#bd1414',
white: '#fff',
};
索引.ts
import colors from './colors';
export default {
button,
colors,
};
我收到以下错误:
Could not find a declaration file for module './colors'. '/xxxxx/styles/config/colors.js' implicitly has an 'any' type.
我对 Typescript 完全陌生,在网上找不到任何教程或示例来清楚地解释我在这里做错了什么。
解决方案
我可以建议您让 Typescript 为您推断类型吗?您可能拥有比使用[key: string]: string
.
如果您不想使用default
导出,我建议您只导出以下内容:
export const Colors = {
black: '#111',
grey: '#999',
green: '#4c8857',
red: '#bd1414',
white: '#fff',
};
然后你会导入import { Colors } from './colors'
如果颜色是colors
文件中唯一/主要的东西,那么您可以继续使用default
如下方式导出:
const colors = {
black: '#111',
grey: '#999',
green: '#4c8857',
red: '#bd1414',
white: '#fff',
};
export default colors;
然后你会导入import Colors from './colors'
推荐阅读
- javascript - How to test truncate filter with Jest?
- java - 如何根据操作系统动态继承?
- c++ - 我应该使用单例设计模式还是其他方式来实现线程安全队列?
- python-3.x - 经常让 CUDA 错误超出内存?
- pointers - 使用 SSE 有效地计算比特总和
- java - Java HttpURLConnection 调用远程服务器并返回 500 状态
- javascript - 如何在javascript的回调中强制重绘元素?
- highcharts - Coloring a region programmatically based by user selection with Highmaps
- jquery - 剑道多选键入的文本与所选项目重叠
- python - 如何将张量流损失函数从单个数字更改为数组