TypeScript错误:类型中缺少属性"0" [英] TypeScript Error: Property '0' is missing in type

查看:103
本文介绍了TypeScript错误:类型中缺少属性"0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的界面

export interface Details {
  Name: [{
    First: string;
    Last: string;
  }];
}   

我有一个可观察的配置变量:

I have an observable config variable:

Configuration: KnockoutObservable<Details> = ko.observable<Details>();

,我想在构造函数中为其分配一个值,如下所示:

and I would like to assign it a value in the constructor as follows:

config = {
  Name: [{
    First: "ABC",
    Last: "DEF"
  },
  {
    First: "LMN",
    Last: "XYZ"
  }]
};

this.Configuration(config);

我收到一个错误:

and I am getting an error:

Types of property 'Name' is incompatible and property '0' is missing in type.
Type '{ First:string; Last:string; }[]' is not assignable to 
type '[{ First: string; Last:string; }]'

我无法控制在其他地方使用的接口.
初始化此配置变量的正确方法是什么?

I don't have control on changing the interface as it is being used elsewhere.
What is the correct way to initialize this config variable ?

谢谢.

推荐答案

我遇到了相同的问题,并通过将界面更改为以下方式解决了这个问题:

I came across the same issue and got around it by changing the interface to:

    interface Details {
        Name: {
            First: string;
            Last: string;
        }[];
    }

我知道您可能不希望更改界面,但希望这对处于这种情况的任何人都有所帮助.

I know you may not want the interface changed but hope this helps for anyone that is in this situation.

这篇关于TypeScript错误:类型中缺少属性"0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆