Type 'CanvasRenderingContext2D | WebGLRenderingContext' is not assignable to type 'CanvasRenderingContext2D'

Matthew James Davis

I have upgraded to Visual Studio 2015 Community with Typescript 1.5 Beta. I am getting the following error.

Type 'CanvasRenderingContext2D | WebGLRenderingContext' is not assignable to type 'CanvasRenderingContext2D'

This is happening on the following line

var canvas: HTMLCanvasElement = $(element).find('canvas').get(0);
var ctx: CanvasRenderingContext2D = canvas.getContext("2d");

I have set TypeScript tools version to both 1.4 and 1.5 and get the same error.

Fenton

From the error message, the return type of getContext appears to be a union type, which means it is either one of CanvasRenderingContext2D or WebGLRenderingContext.

The compiler cannot tell which, so you need to help it out:

var ctx = <CanvasRenderingContext2D> canvas.getContext("2d");

However, if I try this with the latest version of everything, this works just fine:

var canvas = <HTMLCanvasElement> $('#example').find('canvas').get(0);
var ctx: CanvasRenderingContext2D = canvas.getContext("2d");

So it looks like something isn't quite right.

The current definition of getContext has a specialized signature for the value "2d" that should give you back a CanvasRenderingContext2D. Here are the three signatures...

  1. "2d" : CanvasRenderingContext2D
  2. "experimental-webgl" : WebGLRenderingContext
  3. other string : CanvasRenderingContext2D | WebGLRenderingContext

It should only give you back the union type if it doesn't recognise the string being passed in.

If your auto-completion doesn't suggest these three overloads when you type the ( after getContext, that may indicate a problem.

Auto-Completion for getContext with 2d argument

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Typescript Type 'string' is not assignable to type

From Dev

Argument type 'A' is not assignable to type 'B'?

From Dev

Uncaught TypeMismatchError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D'

From Dev

drawImage() of canvasrenderingcontext2d not displaying img

From Dev

JIC - Java Image Compressor : Failed to execute 'drawImage' on 'CanvasRenderingContext2D': No function was found that matched the signature

From Dev

CanvasRenderingContext2D flip transformation

From Dev

Angular 2 bootstrap function gives error "Argument type AppComponent is not assignable to parameter type Type"

From Dev

IntelliJ and Angular 2 Argument Type Not Assignable Errors

From Dev

Angular2 Error 'Argument of type '{}' is not assignable...'

From Dev

ionic 2 "Type '{}' is not assignable to type 'any[]"

From Dev

Transparency groups in CanvasRenderingContext2D

From Dev

Is there any method for `WebGLRenderingContext` as in `CanvasRenderingContext2D.getImageData()`?

From Dev

Uncaught TypeError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The provided double value is non-finite

From Dev

Type \'Observable<{}>\' is not assignable to type in angular 2

From Dev

Type '{}' is not assignable to type 'string'

From Dev

angular2 Type 'Subject<{}>' is not assignable to type '() => Subject<number>'

From Dev

Angular2 : Type 'Subscription' is not assignable to type

From Dev

Angular 2- Error: Type 'AbstractControl' is not assignable to type 'AbstractControl'

From Dev

Serialising a JavaScript CanvasRenderingContext2D states array

From Dev

Uncaught TypeMismatchError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D'

From Dev

Merge multiple paths in CanvasRenderingContext2D to fill and stroke as a set

From Dev

CanvasRenderingContext2D resetTransform and google closure compiler

From Dev

Type '{}' is not assignable to type 'string'

From Dev

angular2 Type 'Subject<{}>' is not assignable to type '() => Subject<number>'

From Dev

Angular 2- Error: Type 'AbstractControl' is not assignable to type 'AbstractControl'

From Dev

Angular2 Error: Type 'number' is not assignable to type 'NumberConstructor'

From Dev

What is the alpha attribute in a CanvasRenderingContext2D?

From Dev

Angular 2 and typescript argument of type response is not assignable

From Dev

Argument of type 'number' is not assignable to parameter of type 'string' in angular2.

Related Related

  1. 1

    Typescript Type 'string' is not assignable to type

  2. 2

    Argument type 'A' is not assignable to type 'B'?

  3. 3

    Uncaught TypeMismatchError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D'

  4. 4

    drawImage() of canvasrenderingcontext2d not displaying img

  5. 5

    JIC - Java Image Compressor : Failed to execute 'drawImage' on 'CanvasRenderingContext2D': No function was found that matched the signature

  6. 6

    CanvasRenderingContext2D flip transformation

  7. 7

    Angular 2 bootstrap function gives error "Argument type AppComponent is not assignable to parameter type Type"

  8. 8

    IntelliJ and Angular 2 Argument Type Not Assignable Errors

  9. 9

    Angular2 Error 'Argument of type '{}' is not assignable...'

  10. 10

    ionic 2 "Type '{}' is not assignable to type 'any[]"

  11. 11

    Transparency groups in CanvasRenderingContext2D

  12. 12

    Is there any method for `WebGLRenderingContext` as in `CanvasRenderingContext2D.getImageData()`?

  13. 13

    Uncaught TypeError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The provided double value is non-finite

  14. 14

    Type \'Observable<{}>\' is not assignable to type in angular 2

  15. 15

    Type '{}' is not assignable to type 'string'

  16. 16

    angular2 Type 'Subject<{}>' is not assignable to type '() => Subject<number>'

  17. 17

    Angular2 : Type 'Subscription' is not assignable to type

  18. 18

    Angular 2- Error: Type 'AbstractControl' is not assignable to type 'AbstractControl'

  19. 19

    Serialising a JavaScript CanvasRenderingContext2D states array

  20. 20

    Uncaught TypeMismatchError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D'

  21. 21

    Merge multiple paths in CanvasRenderingContext2D to fill and stroke as a set

  22. 22

    CanvasRenderingContext2D resetTransform and google closure compiler

  23. 23

    Type '{}' is not assignable to type 'string'

  24. 24

    angular2 Type 'Subject<{}>' is not assignable to type '() => Subject<number>'

  25. 25

    Angular 2- Error: Type 'AbstractControl' is not assignable to type 'AbstractControl'

  26. 26

    Angular2 Error: Type 'number' is not assignable to type 'NumberConstructor'

  27. 27

    What is the alpha attribute in a CanvasRenderingContext2D?

  28. 28

    Angular 2 and typescript argument of type response is not assignable

  29. 29

    Argument of type 'number' is not assignable to parameter of type 'string' in angular2.

HotTag

Archive