gpt4 book ai didi

typescript - Vue + Typescript - 无效类型

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:23 28 4
gpt4 key购买 nike

使用 typescript 2.8.3、ts-loader 3.5.0(由于使用了 webpack 2)和 vue 2.5.16 尝试在 SFC 中定义组件时出现错误,如下所示:

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
name: 'TestComponent',

props: {
options: {
type: Array,
default: () => [],
}
},

computed: {
getOptions() {
return this.options
},
}
})
</script>

这是错误:

TS2339: Property 'options' does not exist on type 'ComponentOptions, DefaultMethods, DefaultComputed, PropsDefinition

这是 tsconfig.json:

{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noImplicitAny": false,
"baseUrl": ".",
"allowJs": true,
"paths": {
"test/*": ["test/*"]
},
"lib": ["dom", "es2016"],
"types": ["node", "jest"]
},
"include": ["resources/assets/js/**/*.ts", "resources/assets/js/**/*.vue"],
"exclude": ["node_modules"]
}

有什么想法吗?如果我设置 "noImplicityThis": false 可以正常工作,但 VSCode 上的自动完成不正确。

最佳答案

不知何故开始工作。将 tsconfig.json 更改为以下内容:

{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": false,
"jsx": "preserve",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"allowJs": true,
"lib": ["dom", "esnext"],
"types": ["node", "jest"]
},
"include": ["resources/assets/js/**/*.ts", "resources/assets/js/**/*.vue"],
"exclude": ["node_modules"]
}

和 vue.d.ts 到:

declare module '*.vue' {
import Vue from 'vue'
export default Vue
}

也许对某人有用。

关于typescript - Vue + Typescript - 无效类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50585112/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com