简体   繁体   English

打字稿强类型void功能

[英]Typescript strongly type void Function

I'm trying to write an interface which takes a function in as a parameter: 我正在尝试编写一个将函数作为参数的接口:

Currently I'm trying this 目前我正在尝试

export interface EditOptions {
     isEditing: boolean;
     save: () => {};
}

I've tried a few things to assign the function: 我尝试了一些分配功能的方法:

editOptions: EditOptions = { isEditing: false, save: this.save };
editOptions: EditOptions = { isEditing: false, save: () => { this.save() } };

Neither work instead I receive this error: 两者都不起作用,但我收到此错误:

在此处输入图片说明

I Know that for now I can use :any but what is the proper way to strongly type a void function 我知道现在我可以使用:any但是强类型键入void函数的正确方法是什么?

interface you can define as : 您可以将接口定义为:

export interface EditOptions {
 isEditing: boolean;
 save: () => void;
 }

and you can use/assign it as : 您可以将其使用/分配为:

editOptions: EditOptions = { isEditing: false, save: () => { this.anyFunction() } };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Typescript 中承诺 function “类型 void 不可分配……” - Promises function in Typescript “type void is not assignable …” 如何在 TypeScript 为空 function 时键入异步 function - How to type async function in TypeScript when its a void function 在打字稿中,返回空函数的函数的返回类型是什么? - In typescript, what is the return type of a function that returns a void function? Typescript 类型 boolean 不可分配给 void - Typescript type boolean is not assignable to void 打字稿功能怪异的空隙|| &amp;&amp;行为 - Typescript Function Weird Void || && Behaviour 打字稿错误:类型&#39;undefined []&#39;不能分配给&#39;void&#39;类型 - Typescript error: Type 'undefined[]' is not assignable to type 'void' 打字稿:类型&#39;Promise &lt;{}&gt;&#39;不能分配给&#39;Promise&#39; <void> “ - Typescript : Type 'Promise<{}>' is not assignable to type 'Promise<void>' 无效类型不能分配给布尔类型(反应/打字稿) - Type Void is Not Assignable to Type Boolean (React/Typescript) 打字稿:输入“承诺”<void> &#39; 不可分配给类型 &#39;void | 破坏者 - Typescript: Type 'Promise<void>' is not assignable to type 'void | Destructor' 类型 'void' 不可分配给类型 '((event: ChangeEvent<htmlinputelement> ) =&gt; void) 反应 TypeScript</htmlinputelement> - Type 'void' is not assignable to type '((event: ChangeEvent<HTMLInputElement>) => void) React TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM