1

I want to tell TypeScript to treat my ref as a canvas element so it is happy about me calling getContext.

So I do this: this.context = (<HTMLCanvasElement>this.$refs.canvas).getContext('2d');

As far as ts is concerned, it's now happy about me calling getContext, however, now I get an eslint error:

Module Warning (from ./node_modules/eslint-loader/index.js):

D:\Projects\image-parser\image-parser\src\components\CanvasPanScale.vue
  51:32  error  Parsing error: '}' expected

✖ 1 problem (1 error, 0 warnings)

It's actually referring to code a few lines down:

this.context = (<HTMLCanvasElement>this.$refs.canvas).getContext('2d');
this.context.imageSmoothingEnabled = false;
if (this.imageToDraw) {
  this.drawImageToCanvas(); // <-- ESLint is angry about this closing parentheses
}

This seems to be related to the fact that I am inside a .vue file rather than a .ts file, and since both html elements and ts types are allowed in the same file, it gets confused by the angle brackets? Removing the angle brackets does fix it.

I can fix the error by doing:

const canvas = this.$refs.canvas as HTMLCanvasElement;
this.context = canvas.getContext('2d');
this.context.imageSmoothingEnabled = false;
if (this.imageToDraw) {
  this.drawImageToCanvas();
}

But I'm creating a variable for no purpose other than to treat it as a canvas, and I feel like the other way shouldn't break ESLint. Anyone know a fix for this? Or am I doing something wrong that I'm not noticing?

0

1 Answer 1

1

Found the answer and figured I'd post in case others have similar issues. I made the mistake of installing the Vue Extension Pack for VSCode, which in turn just installs like 8 other plugins. One of those plugins is an ESLint config that completely breaks TypeScript linting and autocomplete. I'm sure somewhere within that giant pack of plugins I could have found some setting to fix it, but rather than deal with that I uninstalled the whole pack then only installed Vuter. Now everything is fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.