Javascript Typescript:函数参数中的并集导致错误
从Flow来看,这过去很好,但在TS中显然不是这样,我在网上找不到合适的例子:Javascript Typescript:函数参数中的并集导致错误,javascript,typescript,Javascript,Typescript,从Flow来看,这过去很好,但在TS中显然不是这样,我在网上找不到合适的例子: type Props = { fnA(string, string | object): void; // ^ Unexpected token, expected "," fnB(string, any): void; // No problems with this } 为函数参数编写联合类型的正确方法是什么?这就是TypeScript语法的特点:在
type Props = {
fnA(string, string | object): void;
// ^ Unexpected token, expected ","
fnB(string, any): void; // No problems with this
}
为函数参数编写联合类型的正确方法是什么?这就是TypeScript语法的特点:在函数类型中,必须为所有参数命名:
type Props = {
fnA(a: string, b: string | object): void;
fnB(string, any): void; // string, any are parameter names, not types
}
在第二个函数fnB
中,string
和any
被混淆地解释为具有隐式any
类型的参数名称,而不是类型-如果启用了--noImplicitAny
选项,则会出现错误