typescript - 如何添加索引签名以导出默认对象
问题描述
第一.ts:
import textFilters from './second'
Object.keys(textFilters).forEach(key => {
Vue.filter(key, textFilters[key])
})
第二个.ts:
export default {
trimDescription: (text: string, length: number): string => {
return text.length > length ? text.substring(0, length - 3) + '...' : text
}
}
我收到关于缺少索引签名的错误。我怎么能把它添加到export default {}
?
更新 1
我可以在 second.ts 中这样做:
interface Keys {
[key: string]: any
}
const obj: Keys = {
trimDescription: (text: string, length: number): string => {
return text.length > length ? text.substring(0, length - 3) + '...' : text
}
}
export default obj
但最初的问题是,我可以这样做export default {}
吗?
解决方案
在对象中,像这样定义索引签名:
{
[index: string]: any;
...rest of your object
}
将 any 替换为您希望对象值具有的实际类型。
推荐阅读
- c# - 在可选关系上强制从 EF Cores Include() 进行内部连接
- ios - 关于名为 SKAction.moveTo 的内置函数的问题
- sql - 根据表格标准对错误进行分类
- ios - 自定义视图未正确显示
- datetime - Tableau Desktop 上日期字段的级联筛选器
- angular - 单击行上的任意位置时如何防止选中复选框?
- python - 无法使用 Get_Workers() 从 Workday 获取员工信息(python)
- python - Python Flask Jinja2 css 样式
- c# - 在 C# 中独立比较值
- c# - 如何将带单引号的字符串传递给 Sql 存储过程