What did you expect to happen?
\nVue instance methods and computed properties to be understood as already bound.
What actually happened?
\n\nHitting this for all uses of computed properties and methods, which are all automatically bound by Vue already. eslint-config-typescript and vue-eslint-parser. Also, vue-tsc misunderstands computed properties as () -> ActualType instead of ActualType resulting in errors like Argument of type '() => ActualType' is not assignable to parameter of type 'ActualType'
error Avoid referencing unbound methods which may cause unintentional scoping of `this`. \nIf your function does not access `this`, you can annotate it with `this: void`, or consider using an arrow function instead @typescript-eslint/unbound-method\neslint.config.js:
import {\n defineConfigWithVueTs,\n vueTsConfigs,\n} from \"@vue/eslint-config-typescript\";\nimport { globalIgnores } from \"eslint/config\";\nimport prettierConfig from \"@vue/eslint-config-prettier\";\nimport standard from \"@vue/eslint-config-standard-with-typescript\";\nimport globals from \"globals\";\nimport tsParser from \"@typescript-eslint/parser\";\nimport vueParser from \"vue-eslint-parser\";\nimport js from \"@eslint/js\";\nimport pluginVue from \"eslint-plugin-vue\";\nimport storybook from \"eslint-plugin-storybook\";\n\nexport default defineConfigWithVueTs(\n globalIgnores([\n \"node_modules\",\n \"dist/\",\n \"components/*/dist/\",\n \"**/index.d.ts\",\n ]),\n js.configs.recommended,\n standard,\n storybook.configs[\"flat/recommended\"],\n vueTsConfigs.recommendedTypeChecked,\n pluginVue.configs[\"flat/recommended\"],\n prettierConfig,\n {\n name: \"our-project/base\",\n files: [\"**/*.{js,jsx,ts,tsx,vue}\"],\n languageOptions: {\n globals: {\n ...globals.node,\n ...globals.browser,\n },\n sourceType: \"module\",\n ecmaVersion: \"latest\",\n parser: vueParser,\n parserOptions: {\n parser: tsParser,\n projectService: true,\n sourceType: \"module\",\n tsconfigRootDir: import.meta.dirname,\n },\n },\n rules: {\n […]\n }\n }\n);tsconfig.json:
{\n \"compilerOptions\": {\n \"target\": \"ES2022\",\n \"module\": \"ESNext\",\n \"moduleResolution\": \"bundler\",\n \"strict\": true,\n \"jsx\": \"preserve\",\n \"esModuleInterop\": true,\n \"allowSyntheticDefaultImports\": true,\n \"forceConsistentCasingInFileNames\": true,\n \"skipLibCheck\": true,\n \"declaration\": true,\n \"declarationMap\": true,\n \"sourceMap\": true,\n \"resolveJsonModule\": true,\n \"verbatimModuleSyntax\": true,\n \"types\": [\"node\", \"vue\", \"vitest/globals\"],\n \"allowJs\": true,\n \"checkJs\": true,\n },\n \"include\": [\n \"**/*.ts\",\n \"**/*.tsx\",\n \"**/*.vue\",\n \"**/*.js\",\n ],\n \"extends\": [\n \"@vue/tsconfig/tsconfig.dom.json\",\n \"@vue/tsconfig/tsconfig.lib.json\"\n ],\n \"exclude\": [\"node_modules\", \"types\", \"dist\"]\n}Sorry, haven't had time to figure out the minimum reproduction (our project is quite large), but I managed to find a couple of fixes/workarounds, one of which was intended for Vue 2 even though we are on the latest Vue 3 and vue-tsc. It's unclear which one(s) are the \"actual\" cause(s), but these helped:
mapState, mapGetters, etc.For computed variables / component properties & methods issue, this worked well for some cases (for example in a component library where the final type for your Vuex or Pinia store can't be specified yet: https://stackoverflow.com/a/70983629/2836299
\ncomputed cache invalidationTrying to invalidate cache for computed variables as described in the guide has been deprecated as of Vue2. It seems that this:
\ntemplate: '<p>message: {{ timeMessage }}</p>',\ncomputed: {\n timeMessage: {\n cache: false,\n get: function () {\n return Date.now() + this.message\n }\n }\n}results in the inferred type for the computed property being invalid, not only because of the no-longer-supported cache property, but also because explicitly defining get() implies that set() will also be provided.
import type MyDependentComponent from \"./MyDependentComponent\";\nimport { defineAsyncComponent, defineComponent } from \"vue\";\n\nexport default defineComponent({\n name: \"MyComponent\",\n components: {\n MyDependentComponent: defineAsyncComponent<typeof MyDependentComponent>( // <-- Important bit\n () => import(\"MyDependentComponent\")\n ),\n },script setup tagThis worked for at least one component, but did break again and required the solutions above. While it reduced noise in my IDE, once other issues were fixed, this resulted in error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.ts-plugin(7056)
@iraklisg opened on Jul 8, 2022:
\n\n","upvoteCount":0,"url":"https://github.com/vuejs/language-tools/discussions/5629#discussioncomment-14447960"}}}As a workaround, using a normal <script> alongside <script setup> that defines a name property makes the error dissappear
\n\n<script setup lang=\"ts\">\n// SEE: https://github.com/vuejs/vue/issues/12637\n</script>\n\n<script lang=\"ts\">\nexport default {\n name: 'HelloWorld.'\n}\n</script>
-
|
I filed a ticket against What did you do? computed: {
alwaysBound(): string {
return "Always bound!";
},
},
methods: {
// Computed properties trigger the error
getAlwaysBound(): string {
const alwaysBound = this.alwaysBound; // <-- ERROR!
return alwaysBound;
},
// As do instance methods
logAlwaysBound(): string {
const alwaysBound = this.getAlwaysBound(); // <-- ERROR!
console.log(this.getAlwaysBound()); // <-- ERROR!
},What did you expect to happen? What actually happened? Hitting this for all uses of computed properties and methods, which are all automatically bound by Vue already.
import {
defineConfigWithVueTs,
vueTsConfigs,
} from "@vue/eslint-config-typescript";
import { globalIgnores } from "eslint/config";
import prettierConfig from "@vue/eslint-config-prettier";
import standard from "@vue/eslint-config-standard-with-typescript";
import globals from "globals";
import tsParser from "@typescript-eslint/parser";
import vueParser from "vue-eslint-parser";
import js from "@eslint/js";
import pluginVue from "eslint-plugin-vue";
import storybook from "eslint-plugin-storybook";
export default defineConfigWithVueTs(
globalIgnores([
"node_modules",
"dist/",
"components/*/dist/",
"**/index.d.ts",
]),
js.configs.recommended,
standard,
storybook.configs["flat/recommended"],
vueTsConfigs.recommendedTypeChecked,
pluginVue.configs["flat/recommended"],
prettierConfig,
{
name: "our-project/base",
files: ["**/*.{js,jsx,ts,tsx,vue}"],
languageOptions: {
globals: {
...globals.node,
...globals.browser,
},
sourceType: "module",
ecmaVersion: "latest",
parser: vueParser,
parserOptions: {
parser: tsParser,
projectService: true,
sourceType: "module",
tsconfigRootDir: import.meta.dirname,
},
},
rules: {
[…]
}
}
);
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"jsx": "preserve",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"resolveJsonModule": true,
"verbatimModuleSyntax": true,
"types": ["node", "vue", "vitest/globals"],
"allowJs": true,
"checkJs": true,
},
"include": [
"**/*.ts",
"**/*.tsx",
"**/*.vue",
"**/*.js",
],
"extends": [
"@vue/tsconfig/tsconfig.dom.json",
"@vue/tsconfig/tsconfig.lib.json"
],
"exclude": ["node_modules", "types", "dist"]
} |
Beta Was this translation helpful? Give feedback.
-
|
As an alternative, you can try tsslint, which has first-party Vue support based on |
Beta Was this translation helpful? Give feedback.
Sorry, haven't had time to figure out the minimum reproduction (our project is quite large), but I managed to find a couple of fixes/workarounds, one of which was intended for Vue 2 even though we are on the latest Vue 3 and
vue-tsc. It's unclear which one(s) are the "actual" cause(s), but these helped:Manually type
mapState,mapGetters, etc.For computed variables / component properties & methods issue, this worked well for some cases (for example in a component library where the final type for your Vuex or Pinia store can't be specified yet: https://stackoverflow.com/a/70983629/2836299
Use of deprecated
computedcache invalidationTrying to invalidate cache for computed variables as de…