VS代码中的Vue数组支持JSDoc TypeScript错误:类型'ArrayConstructor'缺少类型'MyCustomType []'的以下属性

问题描述 投票:0回答:1

受“Why I no longer use TypeScript with React and why you might want to switch too”的“使用JSDoc进行类型检查”部分的启发,我使用的是Vue CLI创建的ES6项目,但没有TypeScript编译或TypeScript代码。

我打开了Visual Studio Code中的“Check JS”设置,它允许您键入检查JS文件,就好像它们是TypeScript一样。通过JSDoc / @type注释为Visual Studio代码“启用”类型检查。我使用以下内容将我的JS代码放在与Vue代码不同的文件中:

<script src="./MyComponent.js"></script>

如果我尝试在这样的组件上设置Array prop的类型:MyComponent.js

 /**
 * @typedef {object} MyCustomType
 * @property {number} myNumberField
 */

export default {
  props: {
    /** @type {Array<MyCustomType>} */
    resultCards: Array
  }
};

我在Visual Studio代码中遇到以下问题(红色下划线):

Type 'ArrayConstructor' is missing the following properties 
from type 'MyCustomType[]': pop, push, concat, join, and 25 more.ts(2740)

有谁知道这个解决方案?我想将我的代码保留为JavaScript,因此我不必将项目转换为TypeScript来获取类型检查。如果没有解决方案,我可能会忽略尝试为Vue道具成员实际设置类型。

另见:Microsoft/TypeScript: JSDoc support in JavaScript

编辑:如果使用TypeScript与Vue,我认为以下链接将有答案:但我正在尝试使用纯JavaScript和JSDoc,然后使用VSCode中的类型检查。

来自https://frontendsociety.com/using-a-typescript-interfaces-and-types-as-a-prop-type-in-vuejs-508ab3f83480

将Object转换为返回接口的函数,如下所示:

props: {   testProp: Object as () => { test: boolean } }
typescript vue.js visual-studio-code jsdoc
1个回答
0
投票

搜索了一些,这次我在这里找到了答案:https://blog.usejournal.com/type-vue-without-typescript-b2b49210f0b

我需要将上面的代码段更改为:

export default {
  props: {
    /** @type {{ new (): MyCustomType[] }} */
    resultCards: Array
  }
};

另一种有效的语法(我更喜欢)是:

export default {
  props: {
    /** @type {{ new (): Array<MyCustomType> }} */
    resultCards: Array
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.