首页 > 解决方案 > TypeScript:隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型

问题描述

我正在尝试将我在 Vue 中用于处理 i18n 从 JavaScript 到 TypeScript 的自定义钩子移动,但我不断收到以下错误:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Languages'. No index signature with a parameter of type 'string' was found on type 'Languages'.

这是代码

标签: typescriptvue.js

解决方案


Languages输入为{ en: object },但我怀疑您希望它是一个字符串字典,可以这样输入:

type Languages = {
  [locale: string]: Record<string, string>;
}

演示


推荐阅读