打字稿:隐式无参数函数类型 [英] Typescript: implicit no params function type

查看:37
本文介绍了打字稿:隐式无参数函数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要作为参数给出的函数的限定类型.我希望这种类型要么是一个没有参数的函数,它返回一个包含参数的 void 函数(Action),要么是它自己返回的 void 函数.

I want the qualified type for the functions that are being given as parameters. This type I expect either to be a function with no parameters that returns a void function (Action) that does includes parameters or the void function it returns itself.

这是我想使用的代码:

interface JsonArray extends Array<string | number | boolean | Date | Json | JsonArray> { }

interface Json {
    [x: string]: string | number | boolean | Date | Json | JsonArray;
}

type Action = (arg1: string, arg2: Json | JsonArray) => void;
type ReturningAction = () => Action;

function required(arg1: string, ...validationFunctions: Array<ReturningAction | Action>) {
    console.log("Test");
}

function oneOf(arg1: string): (arg1: string, arg2: Json | JsonArray) => void {
    return (arg1: string, arg2: Json | JsonArray) => {
        console.log("Testing");
    }
}

function notEmpty(): (arg1: string, arg2: Json | JsonArray) => void {
    return (arg1: string, arg2: Json | JsonArray) => {
        console.log("Empty");
    }
}

required("field", oneOf); // Shouldn't be accepted
required("field", oneOf("test")) // Should be accepted
required("field", notEmpty); // Should be accepted

然而,TypeScript 忽略了函数定义中超出预期的额外参数.这能解决吗?

However it seems, TypeScript ignores extra parameters within the function definition than it expects. Can this be solved?

即使我执行以下操作:

function required(arg1: string, ...validationFunctions: Array<(arg1: string, arg2: Json | JsonArray) => void>) {
    console.log("Test");
}

required("field", oneOf); // Shouldn't be accepted
required("field", oneOf("test")) // Should be accepted
required("field", notEmpty); // Shouldn't be accepted
required("field", notEmpty()); // Should be accepted

出于某种原因所有都被接受,但只有被调用的函数适用.

All get accepted for some reason, however only the called functions apply.

推荐答案

我想你已经注意到了,TypeScript 允许你传递更少参数的函数(例如,oneOf() 接受一个参数)到需要更多参数的函数(在这种情况下,validationFunctions[0] 应该接受两个参数,假设它是一个 Action).那是因为函数总是可以自由地忽略传入的额外参数,并且在那里导致错误会导致其他地方出现烦人的错误(如 TypeScript 常见问题解答的链接部分所述).

As I think you noted, TypeScript allows you to pass a function of fewer parameters (e.g., oneOf() takes one parameter) to something that expects a function of more parameters (in this case, validationFunctions[0] should take two parameters, assuming it's an Action). That's because a function is always free to ignore extra parameters passed in, and causing an error there would lead to annoying errors elsewhere (as the linked section of the TypeScript FAQ explains).

以下假设您正在使用 strictNullChecks 编译器选项.

The following assumes you're using the strictNullChecks compiler option.

一种解决方法是声明您的 oneOfnotEmpty 函数,以便它们正常工作但不能被解释为 Action 值:

One workaround is to declare your oneOf and notEmpty functions so that they work as normal but fail to be interpreted as Action values:

function oneOf(arg1: string, nope?: never): (arg1: string, arg2: Json | JsonArray) => void {...}

function notEmpty(nope?: never): (arg1: string, arg2: Json | JsonArray) => void {...}

注意在每种情况下都添加了 nope 参数.现在你应该在你期望的地方得到错误.由于 nope 是一个可选参数,您可以将其省略,并且由于它属于 never 类型,因此您几乎必须将其省略.所以它并没有真正改变函数的使用方式.

Notice the addition of the nope parameter in each case. Now you should get errors where you expect them. Since nope is an optional parameter, you can leave it out, and since it's of type never, you pretty much have to leave it out. So it doesn't really change the way the functions can be used.

还有其他修复,但它们更重……例如,您可以使 ActionReturningAction 成为包含您想要的函数的非函数类型打电话.

There are other fixes but they are heavier-weight... for example, you can make Action and ReturningAction a non-function type which contains the function you want to call.

希望有所帮助.

这篇关于打字稿:隐式无参数函数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆