【发布时间】:2017-06-01 12:21:15
【问题描述】:
假设我有一个function type interface,我想用它来强制执行特定函数的特定签名。
有没有办法强制函数的最后一个参数必须是特定类型?
有没有办法做这样的事情:
interface SomeType { /*...*/ }
/**
* this is the function type I want to enforce
* I want the last argument of any `Modifcation` to be `SomeType`
*/
interface Modification {
(...args: [...firstArgs: any[], lastArgument: SomeType]): void
}
// enforcing `Modification`
const someModification: Modification = (firstParam: string, second: number, third: SomeType) => {
/*...*/
}
我知道打字稿使用spread syntax 和tuple types,但上面的伪代码不起作用。
有没有办法做到这一点?
编辑:
免责声明:我认为没有办法做我想做的事,因此这个问题可能不是完全有建设性的。
上下文:
我有一组类,我想对这些类的特定方法强制执行某种约定。我可以通过声明另一个interface and saying that my class implements that interface 来强制某个方法必须符合function type。
例如
interface Convention {(arg0: string, arg1: number): Something}
interface MyClassMethodRequirements {
myMethod: Convention,
myOtherMethod: Convention
}
class MyClass implements MyClassMethodRequirements {
myMethod(a: string, b: number) { return new Something(); }
myOtherMethod(label: string, aNumber: number) { return new Something(); }
otherMethod() { return 'just another non-conforming method' }
}
我想要达到的目标:
上面的Convention 只是一个例子。我要强制执行的是断言为Convention 的方法必须具有/考虑两个参数。
例如(伪代码)
interface NewConvention {(firstArg: any, lastArg?: RequiredType): Something}
interface MyClassMethodRequirements {
myMethod: NewConvention,
myOtherMethod: NewConvention
}
class MyClass implements MyClassMethodRequirements {
myMethod(a: string, b?: RequiredType) { return new Something(); }
// i want an error here because `myOtherMethod` doesn't list `lastArg: RequiredType` in it's parameters
myOtherMethod(label: string) { return new Something(); }
otherMethod() { return 'just another non-conforming method' }
}
对于更多上下文,NewConvention 的 lastArg 应该是可选的覆盖。
再说一次,我不认为我想要达到的目标是可能的,所以这可能没有建设性。
感谢您的帮助:)
【问题讨论】:
-
我很确定目前这是不可能的(检查这个问题:github.com/Microsoft/TypeScript/issues/1336),但你为什么不把最后一个参数移到第一个参数并避免这个问题?
-
@NitzanTomer 最后一个参数在设计上是可选的。真可惜 :( 但老实说,我对打字稿印象深刻,所以我不能抱怨那么多
-
拿
?怎么样?
标签: typescript