vue 开发中常见 ts 问题及解决方案

1,定义对象后扩展对象属性报错

报错详情:Property ‘id’ does not exist on type ‘query’.Vetur(2339)

例如:

let query = {name: 'aa'}
if (id) {
    query.id = id // Property 'id' does not exist on type 'query'.Vetur(2339)
}

解决方法,添加代码:

interface queryObject {
    name: String
    [prop: string]: any
}
let query: queryObject  = {name: 'aa'}
if (id) {
    query.id = id
}
    原文作者:Jaywin
    原文地址: https://segmentfault.com/a/1190000019706085
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞