最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript strongly type void Function - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Jul 27, 2017 at 3:08 johnny 5johnny 5 21k52 gold badges136 silver badges213 bronze badges 1
  • 1 Does the troll care to explain the down vote – johnny 5 Commented Jul 27, 2017 at 3:19
Add a ment  | 

2 Answers 2

Reset to default 6

Hidden away from the docs it exists VoidFunction:

interface Example {
 save: VoidFunction;
}

const example: Example = {save: () => { this.saveForReal(); } };

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() } };
发布评论

评论列表(0)

  1. 暂无评论