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

TypeScript嵌套查找字典类型适配问题求助

问题:TypeScript类型不匹配,无法适配字典结构的JSON数据

我需要在客户端处理大量数据,出于性能考虑打算使用字典。我有如下简化后的JSON数据结构(Values和Styles可包含更多数据):

{
  "StyleNames": [
    "Foo",
    "Bar",
    "Blah"
  ],
  "Styles": {
    "Foo": {
      "Dimensions": {
        "X": {
          "Values": {
            "0": 123,
            "10": 456,
            "50": 789
          }
        },
        "Y": {
          "Values": {
            "0": 123,
            "10": 456,
            "50": 789
          }
        }
      }
    },
    "Bar": {
      "Dimensions": {
        "X": {
          "Values": {
            "0": 123,
            "10": 456,
            "50": 789
          }
        },
        "Y": {
          "Values": {
            "0": 123,
            "10": 456,
            "50": 789
          }
        }
      }
    }
  }
}

我的目标是能通过如下方式访问指定值:

import data from "./data.json";

let lookupData: ILookupData = data;
let value: number =  lookupData.Styles['Foo'].Dimensions['Y'].Values['10'];  // 获取对应数值

但在let lookupData...这一步收到类型不兼容错误:

"Type '{ StyleNames: string[]; Styles: { Foo: { Dimensions: { X: {
Values: { "0": number; "10": number; "50": number; }; }; Y: {
Values: { "0": number; "10": number; "50": number; }; }; }; };
Bar: { Dimensions: { X: { Values: { "0": number; "10": number;
"50": number; }; }; Y: { ...; }; }; }; }; }; }' is not assignable to
type 'ILookupData'.

Types of property 'Styles' are incompatible.

Type '{ Foo: { Dimensions: { X: { Values: { "0": number; "10":
number; "50": number; }; }; Y: { Values: { "0": number; "10":
number; "50": number; }; }; }; }; Bar: { Dimensions: { X: { Values:
{ "0": number; "10": number; "50": number; }; }; Y: { Values: {
"0": number; "10": number; "50": number; }; }; }; }; }' is not
assignable to type 'IRootStyle'.

Property '"Foo"' is incompatible with index signature.

Type '{ Dimensions: { X: { Values: { "0": number; "10": number;
"50": number; }; }; Y: { Values: { "0": number; "10": number;
"50": number; }; }; }; }' is not assignable to type 'IStyle'.

Types of property 'Dimensions' are incompatible.

Type '{ X: { Values: { "0": number; "10": number; "50": number;
}; }; Y: { Values: { "0": number; "10": number; "50": number; };
}; }' is not assignable to type 'Record<string, IDimension>'.

Property '"X"' is incompatible with index signature.

Type '{ Values: { "0": number; "10": number; "50": number; }; }'
is not assignable to type 'IDimension'.

Types of property 'Values' are incompatible.

Type '{ "0": number; "10": number; "50": number; }' is not
assignable to type 'Record<string, IValue>'.

Property '"0"' is incompatible with index signature. Type 'number'
is not assignable to type 'IValue'.

我尝试了各种Record<T,U>关联数组/字典方案,但仍无法找出适配的接口模型。当前的接口定义如下:

export interface IValue {
    [key: string] : number;
}

export interface IDimension {
    Values: Record<string, IValue>
}

export interface IStyle {
    Dimensions: Record<string, IDimension>
}

export interface IRootStyle {
    [Name: string]: IStyle;
}

export interface ILookupData {
    Styles: IRootStyle;
    StyleNames: string[];
}

请问我遗漏了什么?


解决方案

核心问题是你多定义了一层不必要的IValue接口,导致类型层级和实际JSON结构不匹配:

在你的JSON里,Values的每个键(比如"0"、"10")直接对应数字值,但你定义的IDimension里,Values是Record<string, IValue>——这意味着每个键对应一个IValue类型的对象(即字符串键对应数字的嵌套字典),和实际结构不符。

正确的接口定义

export interface IDimension {
    Values: Record<string, number>; // 直接映射字符串键到数字,无需IValue
}

export interface IStyle {
    Dimensions: Record<string, IDimension>;
}

export interface IRootStyle {
    [name: string]: IStyle;
}

export interface ILookupData {
    Styles: IRootStyle;
    StyleNames: string[];
}

说明

  • 移除多余的IValue接口,因为JSON中Values的结构就是字符串键直接对应数字,不需要额外嵌套。
  • IDimension里的Values改为Record<string, number>,完全匹配JSON的键值对结构。
  • 其余层级的定义保持不变,Record<string, IDimension>和索引签名可以正确适配动态的维度、样式名称。

验证

修改后即可正常赋值并访问数据:

import data from "./data.json";

let lookupData: ILookupData = data;
let value: number = lookupData.Styles['Foo'].Dimensions['Y'].Values['10']; // 正确获取456

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 17:44:59