I'm using Deno to compile some TypeScript and then serve it as part of a web page, so that it is run on the browser side. I'm trying to use a canvas element on the client side, and for that I need types like CanvasRenderingContext2D
or CanvasGradient
, which are defined in lib.dom.d.ts, but they are not available: Deno compilation gives errors like TS2304 [ERROR]: Cannot find name 'CanvasRenderingContext2D'.
. (On the other hand, type Path2D
(defined in the same file) does not cause problems.)
Note: I know the types will exist in runtime when the code runs in the browser, but I want Deno to know about them in compile time.
I've tried including the .d.ts file somehow. Things I tried:
- specifying
"libs": ["deno.window", "esnext"]
etc. in the compiler options (in deno.json). - importing the type like this:
/// <reference types="https://raw.githubusercontent.com/microsoft/TypeScript/main/lib/lib.dom.d.ts" />
- or this:
// @deno-types="https://raw.githubusercontent.com/microsoft/TypeScript/main/lib/lib.dom.d.ts"
Some of these attempts didn't work at all, and some weren't even parsed apparently. Looks like I don't understand how Deno loads the type definitions, e.g. where does it load the Path2D
type declarations from. How to fix this?
Deno.emit
(which is currently the same algorithm used bydeno bundle
in the CLI). – Erlindaerline