繁体   English   中英

如何在 Typescript 接口中定义 onSubmit 的类型?

[英]How can you define a type for onSubmit in a Typescript interface?

在我的 React 应用程序中,我有一个界面,在该界面中我还可以选择指定 onSubmit 方法并使用 any 类型(我想避免)。 所以我正在寻找一种方法来定义 onSubmit 方法的类型。

这是我的界面:

export interface CustomInterface {
  product?: ProductClass;
  loggedInUser: UserClass;
  headerTitle?: (s: string) => any;
  onSubmit?: any; // Here I need a better type
}

如何优化这个界面?

尝试这个:

export interface CustomInterface {
  product?: ProductClass;
  loggedInUser: UserClass;
  headerTitle?: (s: string) => any;
  onSubmit?: (data:Define your type here like string,boolean and so on)=>void // Here I need a better type
}

我建议做这样的事情:

export interface CustomInterface {
  product?: ProductClass;
  loggedInUser: UserClass;
  headerTitle?: (s: string) => any;
  onSubmit?(): void;
}

或者让我们说 onSubmit function 需要一个字符串参数:

export interface CustomInterface {
  product?: ProductClass;
  loggedInUser: UserClass;
  headerTitle?: (s: string) => any;
  onSubmit?(s: string): void;
}

你在提交时声明的这个类型为 function。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM