我想定义一个函数类型,该函数类型允许在所需参数之前的任意参数 示例:
type Op = (...args, required: A) => void
我希望具有以下行为:
const foo:Op = (a.b,c, required: A) => void // this should compile
const foo:Op = (a.b,c:A) => void // this should compile
const foo:Op = (a.b,c) => void // this should not compile
我尝试过:
type Op = (required:A) => string
但是这些会引发类型不匹配错误:
let foo: Op = (a, b:A) => {return ""} //error
let foo: Op = (a, required:A) => {return ""} // error
我尝试了其他方法来定义希望幸运的类型
type Op = (...args: any[], required:A) => string //error
interface Op {
(...args: any[], required:A): string //error
}
他们都没有工作。
有没有办法在Typescript中做到这一点?还是我在尝试某种在概念上不可调和的东西?
答案 0 :(得分:1)
我认为你不走运。甚至做这样的事情...
interface A {
x: number
}
type Op = ((a, b, c, required: A) => string)
| ((a, b, required: A) => string)
| ((a, required: A) => string)
| ((required: A) => string);
let foo: Op = (a, b:A) => "";
let bar: Op = (a, b:number) => ""; // not an error :-(