简体   繁体   English

有没有办法让 Typescript 在具有不同参数计数时考虑函数类型不等价?

[英]Is there a way to make Typescript consider function types non-equivalent when they have different parameter counts?

Consider the following code:考虑以下代码:

function typeTest(callback:(item1:number, item2:string)=>number):number {
    return callback(5, "foo");
}

//This works:
typeTest((num : number, str : string) => { return num;} )
//But surprisingly, so does this, even though the supplied callback doesn't have enough parameters
typeTest((num : number) => { return num;} )

I'm trying to tell the compiler that the function "typeTest" takes a callback with two parameters.我试图告诉编译器函数“typeTest”需要一个带有两个参数的回调。 However, if I give it a callback with fewer parameters, it still works.但是,如果我给它一个参数较少的回调,它仍然有效。 I presume Typescript thinks that the function defined as "(num : number) => number" implements the function type of "(number, item2)", basically ignoring the second parameter.我假设Typescript认为定义为“(num:number)=> number”的函数实现了“(number,item2)”的函数类型,基本上忽略了第二个参数。 That makes some sense, but we re-defined a callback type to take more parameters, and expected the compiler to tell us where we'd forgotten to re-implement the old callbacks.这是有道理的,但我们重新定义了一个回调类型以接受更多参数,并希望编译器告诉我们忘记重新实现旧回调的位置。 I might be running into a fundamental property of Typescript's type system, but is there a way to tell the compiler, "No really - the passed in function needs to take all of these parameters"?我可能会遇到 Typescript 类型系统的一个基本属性,但是有没有办法告诉编译器,“不是真的 - 传入的函数需要采用所有这些参数”?

Edit : As for the motivation: In our case, the we used to have a "setSortFunction" method that expected a method that took a single item and returned an integer.编辑:至于动机:在我们的例子中,我们曾经有一个“setSortFunction”方法,该方法期望一个接受单个项目并返回一个整数的方法。 If you compared that integer amongst all of the items, you could sort the array.如果您在所有项目中比较该整数,则可以对数组进行排序。 Later we changed it to accept the more standard, "take two items, compare them, and return -1, 0, or 1".后来我们将其更改为接受更标准的“取两个项目,比较它们,然后返回 -1、0 或 1”。 So, now we know for a fact that our sort functions passed in need to take two items and compare them.所以,现在我们知道一个事实,我们传入的排序函数需要获取两个项目并比较它们。 If they don't, they are probably broken.如果他们不这样做,他们可能已经坏了。 So, we wanted a way to enforce the requirement that the sortFunction passed in accepted two parameters.因此,我们想要一种方法来强制要求 sortFunction 传入接受两个参数。

However, if I give it a callback with fewer parameters, it still works.但是,如果我给它一个参数较少的回调,它仍然有效。

Yes.是的。 This is by design.这是设计使然。 Think about it : it's safe for the caller to call a function that doesn't care about the last few arguments.想一想:调用者调用一个不关心最后几个参数的函数是安全的。 This is the way JS works in conventional code bases.这就是 JS 在传统代码库中的工作方式。

This will clarify it a bit more : https://github.com/Microsoft/TypeScript/wiki/Type%20Compatibility#comparing-two-functions这将澄清一点: https : //github.com/Microsoft/TypeScript/wiki/Type%20Compatibility#comparing-two-functions

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

相关问题 打字稿中不同类型的函数参数 - function parameters with different types in typescript 找到长度为k的向量的所有非等价置换,取n个可能值 - Finding all non-equivalent permutations for vectors of length k taking n possible values Typescript function参数和返回参数不同 - Typescript function parameter and return parameter different 为什么打字稿中的这两种功能类型不同? - Why are these two function types in typescript different? JavaScript有function个参数类型吗? - Does JavaScript have function parameter types? 函数模板 - 当使用相同类型调用时,编译器选择具有不同参数类型的函数 - Function templates - compiler choosing function with different parameter types when calling with the same type 声明和定义中的不同函数参数类型 - Different function parameter types in declaration and definition 有没有一种方法可以在R中声明函数参数类型? - Is there a way to declare function parameter types in R? 如何为同一个功能使用不同的输入类型? - How to have different input types for the same function? 相同的代码针对不同的数据类型重复多次; 有没有办法使一个函数可以处理所有可能的类型? - Exact same code repeated multiple times for different data types; Any way to make one function that can handle all possible types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM