javascript - 如何在 Typescript 中转换类的方法
问题描述
我的目标是能够在不重复参数类型和返回类型的情况下声明一个方法。我为此使用了打字稿Type
。
但是现在,如何告诉 Typescriptheyy Typescript, this method sum is of type IBar
type IBar = (x:number, y: number)=>number;
class Foo {
sum(x,y){
return x+y;
} as IBar
}
我试过了
type IBar = (x:number, y: number)=>number;
class Foo {
sum:IBar=(x,y)=>{
return x+y;
}
}
但不幸的是,这将 javascript 代码转换为:
class Foo {
constructor() {
this.sum = (x, y) => {
return x + y;
};
}
}
我正在寻找可以转换为:
class Foo {
sum(x, y) {
return x + y;
}
}
解决方案
推荐阅读
- postgresql - 列到行和值作为新列 postgresql
- python-3.x - 使用boto3,从整个文件夹或文件从一个s3存储桶复制到同一区域的另一个存储桶时,如何提供访问密钥和秘密访问密钥?
- sql - SQL:将小跨度变成1个大的连续跨度
- java - popuppanel 由另一个 popuppanel 上的超链接的 clickhandler 调用
- java - XSD Schema 元素可以是两种类型之一
- outlook-web-addins - 如何从 OWA 上的服务器刷新 Office.context.roamingSettings?
- d3.js - D3 缩放和鼠标悬停工具提示
- amazon-web-services - Cloud Formation - 无法上传模板文件
- python - 如何迭代定义数据框并将它们像 csv 一样导出
- flutter - 尝试使用类型转换获取 Firestore 快照