【发布时间】:2020-11-24 02:49:28
【问题描述】:
使用 Vue 组合 API,我们创建了以下可组合:
import { computed, reactive, SetupContext, ref } from '@vue/composition-api'
export const useApplications = (root: SetupContext['root']) => {
const applications = reactive({
1: {
name: ref(root.$t('app1.name')),
description: ref(root.$t('app1.description')),
formName: 'app1form',
},
2: {
name: ref(root.$t('app2.name')),
description: ref(root.$t('app2.description')),
formName: 'app2form',
},
})
const getApplication = (id: string) => {
return applications[id]
}
return {
applications: computed(() => applications),
getApplication,
}
}
虽然代码运行良好,但它会产生 TS 错误:
@typescript-eslint/no-unsafe-return:任何类型值的不安全返回
当悬停在applications 部分时,很明显 typescript 可以识别除属性名称 (1) 之外的类型:
我们是否需要创建一个interface 来解决这个问题,我们是否必须重新定义界面中的每个属性?我试过这样的东西,但它是不正确的:
interface IApplication {
[key]: string {
name : string
description: string
formName: string
}
}
【问题讨论】:
-
出于好奇,为什么要使用带有数字键的对象而不是数组?
-
对于 limitations 在 Vue 2 的组合 API 中带有数组
-
如果您知道所涉及的库,我将不得不相信这是一个答案。否则,它似乎无法解释任何事情。
-
我第一次使用
array。但是 Vue 2 中的 Vue 反应性系统有 issues 使用这种方法。无论如何感谢您的帮助,非常感谢。
标签: javascript typescript vue.js