首页 > 解决方案 > 默认导出的 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 完全陌生,在网上找不到任何教程或示例来清楚地解释我在这里做错了什么。

标签: javascripttypescript

解决方案


我可以建议您让 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'


推荐阅读