You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript声明数组对象类型报找不到Dictionary类型错误

报错原因

抛出Cannot find name 'Dictionary'.ts(2304)的核心原因是类型引用的作用域不匹配:

  • 你将Dictionary类型声明在API命名空间下,该类型不属于全局作用域,无法直接通过Dictionary名称访问
  • 同一段代码中你引用CurrentUser时使用了带命名空间前缀的API.CurrentUser写法,Dictionary属于同一个命名空间下的类型,同样需要加API.前缀才能正确引用
    此外你声明的Dictionary类型本身存在语法问题:对象属性未指定类型,在TS严格模式下会触发额外的类型错误,需要为每个属性补充对应的类型标注。
修复代码
  1. 修正返回值类型中的字段引用,给Dictionary加上命名空间前缀:
export async function getInitialState(): Promise<{
  settings?: Partial<LayoutSettings>;
  currentUser?: API.CurrentUser;
  loading?: boolean;
  dictionary?: API.Dictionary[]; // 此处补全API.前缀
  fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
  fetchDictionary?: () => Promise<API.DictionaryList | undefined>;
}> {}
  1. 修正命名空间内的Dictionary类型定义,为属性补全类型(属性类型可根据实际业务需求调整):
declare namespace API {
  type Dictionary = {
    key: string;
    value: string;
    dict_type: string;
  };
}
关于数组对象类型的支持

TypeScript原生支持数组对象类型声明,属于基础类型能力,常见的两种等价写法如下:

  • 简写语法:类型[],例如API.Dictionary[]
  • 泛型语法:Array<类型>,例如Array<API.Dictionary>
    如果不需要单独抽离类型,也可以直接内联声明结构,例如:{ key: string; value: string }[]

内容的提问来源于stack exchange,提问作者Dolphin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.10 16:15:46