源有 X 个元素,但目标只允许 1 个 [英] Source has X element(s) but target allows only 1

查看:23
本文介绍了源有 X 个元素,但目标只允许 1 个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打字稿编译显示此错误:

The typescript compile show this error:

源有 X 个元素,但目标只允许 1 个.

Source has X element(s) but target allows only 1.

export const FooMapping: [{id: FooOperation, display: string}] = [
    { id: FooOperation.Undefined, display: 'Nothing' },
    { id: FooOperation.A, display: 'I'm A' },
    { id: FooOperation.B, display: 'I'm B' }
];

export enum FooOperation {
    Undefined = 0,
    A = 1,
    B = 2,
}

定义{id: FooOperation, display: string}的特殊对象数组的正确方法是什么?

What is the correct way to define an array of special object of {id: FooOperation, display: string}?

推荐答案

[{id: FooOperation, display: string}] 定义了一个 元组 正好是一个元素.

[{id: FooOperation, display: string}] defines a tuple of exactly one element.

试试:

export const FooMapping: Array<{id: FooOperation; display: string}> = [
    { id: FooOperation.Undefined, display: 'Nothing' },
    { id: FooOperation.A, display: "I'm A" },
    { id: FooOperation.B, display: "I'm B" }
];

或者想想类似的事情:

interface Foo {
  id: FooOperation;
  display: string;
}

export const FooMapping: Foo[] = [...];

这篇关于源有 X 个元素,但目标只允许 1 个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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