【发布时间】:2019-07-26 02:35:47
【问题描述】:
TypeScript 3.5.1 版
我有一个需要传入参数的函数的简单类型。但是,当我声明该类型的函数时,如果没有指定参数,TypeScript 编译器不会抱怨。这是一个简单的例子。
interface IArgs {
foo: number;
}
type MyFunc = (args: IArgs) => Promise<any>;
// why doesnt this complain?
// there is no first argument specified.
const fn: MyFunc = async() => { };
/*
uncommenting the following line will make the ts compiler
complain like it should, but it should've complained in
the above declaration as well?
*/
// fn();
如何让 TypeScript 编译器抱怨函数声明中缺少参数?
【问题讨论】:
-
那是intended behavior;是否有某些原因导致您需要它无法编译?它的行为几乎与
async(args: IArgs) => {}完全相同,这是一个忽略其参数的函数。 -
是的,它看起来确实是重复的。至于为什么,这只是我有疑问的一种奇怪行为。感谢您提供另一个问题的链接。
标签: typescript