typescript 打字稿和传播运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34179897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript and spread operator?
提问by Amala James
function foo(x:number, y:number, z:number) {
console.log(x,y,z);
}
var args:number[] = [0, 1, 2];
foo(...args);
Why am i getting getting this error in Typescript Playground???
为什么我在 Typescript Playground 中收到这个错误???
Supplied parameters donot match any signature of call target.
提供的参数与调用目标的任何签名都不匹配。
回答by Fenton
So there is a little clause you may have missed:
因此,您可能错过了一个小条款:
Type checking requires spread elements to match up with a rest parameter.
类型检查需要扩展元素与其余参数相匹配。
Without Rest Paramater
无休息参数
But you can use a type assertion to go dynamic... and it will convert back to ES5 / ES3 for you:
但是你可以使用类型断言来动态......它会为你转换回 ES5 / ES3:
function foo(x:number, y:number, z:number) {
console.log(x,y,z);
}
var args:number[] = [0, 1, 2];
(<any>foo)(...args);
This results in the same apply
function call that you'd expect:
这将导致apply
您期望的相同函数调用:
function foo(x, y, z) {
console.log(x, y, z);
}
var args = [0, 1, 2];
foo.apply(void 0, args);
With Rest Parameter
带休息参数
The alternative is that it all works just as you expect if the function accepts a rest parameter.
另一种选择是,如果该函数接受一个 rest 参数,它就会像您期望的那样工作。
function foo(...x: number[]) {
console.log(JSON.stringify(x));
}
var args:number[] = [0, 1, 2];
foo(...args);
回答by Ogglas
I think @Fenton explains it very well but I would like to add some more documentation and possible solutions.
我认为@Fenton 解释得很好,但我想添加更多文档和可能的解决方案。
Solutions:
解决方案:
Function overload. I prefer this solution in this case because it keeps some kind of type safety and avoids ignore and any. The original method and function call does not need to be rewritten at all.
函数重载。在这种情况下,我更喜欢这个解决方案,因为它保持某种类型安全并避免忽略和任何。原来的方法和函数调用根本不需要重写。
function foo(...args: number[]): void
function foo(x: number, y: number, z: number) {
console.log(x, y, z);
}
var args: number[] = [0, 1, 2];
foo(...args);
Use @ts-ignore
to ignore specific line, TypeScript 2.3
使用@ts-ignore
忽略特定行,打字稿2.3
function foo(x: number, y: number, z: number) {
console.log(x, y, z);
}
var args: number[] = [0, 1, 2];
// @ts-ignore
foo(...args);
Use as any.
随便用。
function foo(x: number, y: number, z: number) {
console.log(x, y, z);
}
var args: number[] = [0, 1, 2];
(foo as any)(...args);
Link with documentation regarding the spread operator:
链接与有关传播运算符的文档:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html
Discussions regarding this:
关于此事的讨论:
https://github.com/Microsoft/TypeScript/issues/5296https://github.com/Microsoft/TypeScript/issues/11780https://github.com/Microsoft/TypeScript/issues/14981https://github.com/Microsoft/TypeScript/issues/15375
https://github.com/Microsoft/TypeScript/issues/5296 https://github.com/Microsoft/TypeScript/issues/11780 https://github.com/Microsoft/TypeScript/issues/14981 https://github .com/Microsoft/TypeScript/issues/15375
回答by Lancer.Yan
function foo(x:number, y:number, z:number) {
console.log(x,y,z);
}
var args:[number, number,number] = [0, 1, 2];
foo(...args);
You can try this. Personally, I think it is the answer that best fits your question scenario.
你可以试试这个。就个人而言,我认为这是最适合您的问题场景的答案。