TypeScript:有没有一种方法可以检查函数的算术性?

TypeScript:有没有一种方法可以检查函数的算术性?,typescript,Typescript,给定以下代码: type Function0<T> = () => T; type Function1<T, A1> = (arg1: A1) => T; type Function2<T, A1, A2> = (arg1: A1, arg2: A2) => T; const foo: Function1<string, any> = () => "hi there"; type Function0=()=>T; 类型F

给定以下代码:

type Function0<T> = () => T;
type Function1<T, A1> = (arg1: A1) => T;
type Function2<T, A1, A2> = (arg1: A1, arg2: A2) => T;

const foo: Function1<string, any> = () => "hi there";
type Function0=()=>T;
类型Function1=(arg1:A1)=>T;
类型Function2=(arg1:A1,arg2:A2)=>T;
const-foo:Function1=()=>“你好”;
我希望得到某种错误,因为我试图断言某个0参数函数是一个接受一个参数的类型

但是,下面的编译非常好。是否有某种方法可以检查这些算术数是否完全匹配?

const foo: Function1<string, any> = () => "hi there";
这个


默认情况下,typescript假定参数较少的函数可以分配给将使用更多参数调用函数的位置,因为额外的参数将被忽略,并且不会带来任何伤害。合理的假设:

const foo: Function1<string, any> = () => "hi there";
foo("Ignored, but why would that be a problem ?")
const-foo:Function1=()=>“你好”;
foo(“被忽略了,但为什么这会是个问题?”)
也就是说,在某些情况下,我们可以强制传入函数的参数数与预期的参数数相同。此场景涉及将函数传递给另一个函数,并在参数太少时使用某些条件类型强制执行错误:

const foo_anyway: Function1<string, any> = (arg1: any) => "hi there";
type IsArg1Valid<T extends (...a: any) => any, E> = Parameters<T>['length'] extends 1 ? {} : E ;
function withFoo<T extends (arg: string) => any>(foo:  T & IsArg1Valid<T, "You need to pass in a function with a first argument">){

}
withFoo(()=> {}) //Type '() => void' is not assignable to type '"You need to pass in a function with a first argument"'.
withFoo(a=> console.log(a)) //ok
withFoo((a, b)=> console.log(a)) 
类型ISARG1有效,E>=参数['length']扩展1?{}:E;
带有foo any>的函数(foo:T&IsArg1Valid){
}
withFoo(()=>{})//类型“()=>void”不可赋值给类型“”,您需要传入一个带有第一个参数的函数“”。
withFoo(a=>console.log(a))//确定
withFoo((a,b)=>console.log(a))


注意如果传入参数较少的函数确实是一个错误,那么在运行时的所有情况下都这样做应该是无害的。唯一的理由可能是调用者可能忽略了有用的传入参数,但这可能无法证明强制所有人始终指定所有参数是合理的。默认情况下,typescript假定可以将参数较少的函数分配给将使用更多参数调用函数的位置,因为额外的争论会被忽略,不会带来任何伤害。合理的假设:

const foo: Function1<string, any> = () => "hi there";
foo("Ignored, but why would that be a problem ?")
const-foo:Function1=()=>“你好”;
foo(“被忽略了,但为什么这会是个问题?”)
也就是说,在某些情况下,我们可以强制传入函数的参数数与预期的参数数相同。此场景涉及将函数传递给另一个函数,并在参数太少时使用某些条件类型强制执行错误:

const foo_anyway: Function1<string, any> = (arg1: any) => "hi there";
type IsArg1Valid<T extends (...a: any) => any, E> = Parameters<T>['length'] extends 1 ? {} : E ;
function withFoo<T extends (arg: string) => any>(foo:  T & IsArg1Valid<T, "You need to pass in a function with a first argument">){

}
withFoo(()=> {}) //Type '() => void' is not assignable to type '"You need to pass in a function with a first argument"'.
withFoo(a=> console.log(a)) //ok
withFoo((a, b)=> console.log(a)) 
类型ISARG1有效,E>=参数['length']扩展1?{}:E;
带有foo any>的函数(foo:T&IsArg1Valid){
}
withFoo(()=>{})//类型“()=>void”不可赋值给类型“”,您需要传入一个带有第一个参数的函数“”。
withFoo(a=>console.log(a))//确定
withFoo((a,b)=>console.log(a))


注意如果传入参数较少的函数确实是一个错误,那么在运行时的所有情况下都这样做应该是无害的。唯一的理由可能是调用者可能会忽略有用的传入参数,但这可能无法证明强制所有人始终指定所有参数是合理的

他们在文章中讨论了这一点。我认为主要的一点是,即使进行了此检查,也无法验证函数是否确实对参数执行了任何操作。例如:
(\u ignore:any)=>“hi there”
具有正确的算术,但在行为上与您的示例相同。他们在中讨论了这一点。我认为主要的一点是,即使进行了此检查,也无法验证函数是否确实对参数执行了任何操作。例如:
(\u ignore:any)=>“hi there”
具有正确的算术性,但在行为上与您的示例相同。该死:(strictFunctionTypes不够严格,唉。不过感谢您的回复。该死:(strictFunctionTypes不够严格,唉。不过谢谢你的响应。哇。很好的技巧。我正在尝试对一个根据回调函数的算术返回不同类型的方法编写更好的类型推断,而不是断言可能在运行时发生的错误。谢谢!!哇。很好的技巧。我正在尝试编写更好的类型推断。)基于回调函数的arity返回不同类型的方法,而不是断言可能在运行时发生的错误。谢谢!!