javascript - 如何将 JavaScript 片段转换为 TypeScript
问题描述
如何将代码从 Javascript 转换为 typescript
const ORIENTATIONS = {
single: () => 0,
'right angled': (tag) => {
return hashWithinRange(tag.text) * 90;
},
multiple: (tag) => {
return hashWithinRange(tag.text) * 15 - 90;
},
};
我收到了这个错误:
const ORIENTATIONS: {
single: () => number;
'right angled': (tag: any) => number;
multiple: (tag: any) => number;
}
元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{ single: () => number; '直角':(标签:任何)=>数字;多个:(标签:任何)=>数字;}'。在“{ single: () => number; 类型上找不到带有“字符串”类型参数的索引签名;'直角':(标签:任何)=>数字;多个:(标签:任何)=>数字;}'.ts(7053)
解决方案
您可以创建一个类
export class AppComponent {
name = "Angular";
ORIENTATIONS = {
single: () => 0,
"right angled": tag => {
return this.hashWithinRange(tag.text) * 90;
},
multiple: tag => {
return this.hashWithinRange(tag.text) * 15 - 90;
}
};
hashWithinRange(value) {
return 45;
}
}
推荐阅读
- wpf - 来自 WPF 客户端的 AspNet.Core 身份验证
- java - 合并两个流以创建一个新流
- html - 如何在 CSS 中弹出顶部
- javascript - Angular 和 Leaflet 在点击时添加标记并获取坐标
- javascript - 即使这是预期的,Knexnest 查询也不返回数组中的数据
- angular - Angular8 创建动态容器,其他模块可以注入它们的组件
- r - 特定值后的 R geom_ribbon
- python - AWS Lambda - 与指数退避的 SQS 集成
- model-associations - 如何在没有外键cakephp3的情况下设置动态模型关联
- r - R - 当只有一行时,列类型从“数字”变为“未知”