繁体   English   中英

可迭代的<xxx> : 为什么不能用[],得用new Array()</xxx>

[英]Iterable<Xxx>: Why can't you use [], you have to use new Array()

type XxxType = 'aaa' | 'bbb' | 'ccc'
interface Xxx {
    type: XxxType
}
interface MyXxx {
    name: string
    xxx: Xxx
}
interface MyObj {
    arr: Iterable<MyXxx>
}

const myObj: MyObj = {
    arr: [ { name: 'aaa', xxx: { type: 'aaa' } } ]
}

//emm....
Type '{ name: string; xxx: { type: string; }; }[]' is not assignable to type 'Iterable<MyXxx>'.
Type 'string' is not assignable to type 'XxxType'. ts[2322]

我不认为我写的有什么问题,但我不确定为什么会出错。 而且我没有得到语法提示。


当我将代码从[]更改为new Array时,没有报错!

const myObj: MyObj = { arr: new Array({ name:'aa', xxx:{ type:'aaa'} }) }

为什么???

我找到了原因。

tsconfig.json:
    "target": "es5" > "es2015"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM