小编rob*_*ert的帖子

用于减少的 TypeScript 类型

你将如何正确声明一个类型?

interface MediaQueryProps {
  [key: string]: number;
}

const size: MediaQueryProps = {
  small: 576,
  medium: 768,
  large: 992,
  extra: 1200
};

export default Object.keys(size).reduce((acc, cur) => {
  acc[cur] = `(min-width: ${size[cur]}px)`;

  return acc;
}, {});
Run Code Online (Sandbox Code Playgroud)

acc[cur] 抱怨是因为

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'
Run Code Online (Sandbox Code Playgroud)

有什么方法可以在不使用任何类型的情况下为此声明类型吗?

typescript

13
推荐指数
3
解决办法
2万
查看次数

标签 统计

typescript ×1