[英]Reusable function type
I'm trying declare a function type and then use that in a class, and in objects, and interfaces like this: 我正在尝试声明一个函数类型,然后在类,对象和接口中使用它,如下所示:
declare function IUpdateMode(mode: Mode): void;
Then I want to use it like this: 然后我要像这样使用它:
type Foo = {
updateMode: IUpdateMode
}
class Foo extends React.Component<any> {
updateRedMode(mode): IUpdateMode {}
}
function updateGreenMode(mode): IUpdateMode {
}
Is this possible in typescript? 打字稿可能吗?
I think what you really want is just this: 我认为您真正想要的就是:
type UpdateMode = (mode: Mode) => void
Now you can use it anywhere as you mentioned, but with some corrections: 现在您可以在提到的任何地方使用它,但需要进行一些更正:
interface IFoo { // interface instead of a type is preferable when possible (e.g. you may extend it)
updateMode: UpdateMode
}
class Foo extends React.Component<any> implements IFoo {
updateMode = (mode: Mode) => {}
}
const updateGreenMode: UpdateMode = (mode) => { // there is no way to restrict a classic function to type alias or interface
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.