简体   繁体   English

将类型数组传递为any []吗?

[英]Passing typed array as any[]?

Why can't I pass a typed array into a function/constructor which takes an any[] ? 为什么我不能将类型化数组传递给采用any[]的函数/构造函数?

typedArray = new MyType[ ... ];
items = new ko.observableArray(typedArray);

Gives me the error: 给我错误:

Supplied parameters do not match any signature of call target 提供的参数与呼叫目标的任何签名都不匹配

ko.observableArray is defined as: ko.observableArray定义为:

interface KnockoutStatic 
{
    observableArray: KnockoutObservableArrayStatic;
}

interface KnockoutObservableArrayStatic
{
    new(value: any[]): KnockoutObservableArray;
}

declare var ko: KnockoutStatic;

How do I pass my MyType[] as an any[] ? 如何将MyType[]作为any[]传递? Is this a problem with covariance? 这是协方差问题吗?

typedArray = new MyType[ ... ];

This isn't how you make a new array in JavaScript or TypeScript (in 0.9.0 this will correctly be a syntax error in the empty array case). 这不是用JavaScript或TypeScript创建新数组的方式(在0.9.0中,这在空数组情况下将是语法错误)。 You should just be able to write: 您应该只能够写:

typedArray = [ ... ];

and have the compiler correctly infer the type as MyType[] . 并让编译器正确推断类型为MyType[] If needed, you can add a type annotation: 如果需要,可以添加类型注释:

typedArray = <MyType[]>[ ... ];

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

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