Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOM types in pyodide lead to build issues with Angular and typescript. #4757

Open
Hephaistos7 opened this issue May 11, 2024 · 3 comments
Open
Labels
bug Something isn't working

Comments

@Hephaistos7
Copy link

馃悰 Bug

When using Angular and building an application including Pyodide with ng serve, the following Error occurs:

Error: node_modules/pyodide/pyodide.d.ts:13:22 - error TS2304: Cannot find name 'HTMLCanvasElement'.
13  setCanvas2D(canvas: HTMLCanvasElement): void;
                        ~~~~~~~~~~~~~~~~~

Error: node_modules/pyodide/pyodide.d.ts:14:17 - error TS2304: Cannot find name 'HTMLCanvasElement'.
14  getCanvas2D(): HTMLCanvasElement | undefined;
                   ~~~~~~~~~~~~~~~~~

Error: node_modules/pyodide/pyodide.d.ts:15:22 - error TS2304: Cannot find name 'HTMLCanvasElement'.
15  setCanvas3D(canvas: HTMLCanvasElement): void;
                        ~~~~~~~~~~~~~~~~~

Error: node_modules/pyodide/pyodide.d.ts:16:17 - error TS2304: Cannot find name 'HTMLCanvasElement'.
16  getCanvas3D(): HTMLCanvasElement | undefined;
                   ~~~~~~~~~~~~~~~~~
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
脳 Failed to compile.

The problem seems to be the non-compatibility of the types "webworker" and "dom". This issue is being discussed in microsoft/TypeScript#20595.

It is suggested that Pyodide should run in a web worker. Web workers use the "webworker" types, therefore cannot relate to the type "HTMLCanvasElement".
And the types "webworker" and "dom" are mutually exclusive, as they contain overlapping code.

To Reproduce

Install pyodide in Angular project using 'npm install pyodide'.

pyodide.worker.ts File:

import { loadPyodide} from 'pyodide';
const pyodide = await loadPyodide();

main.ts File:

let pyodideWorker = new Worker(new URL('./pyodide.worker.ts', import.meta.url), { type: 'module' });

tsconfig.json File;

{
  "compilerOptions": {
    "lib": [
      "es2015",
      "dom"
    ],
    ...
  }
}

tsconfig.worker.json File:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "lib": [
      "es2018",
      "webworker"
    ],
    "types": [
      "node",
    ],
  },
  "include": [
    "src/**/*.worker.ts"
  ],
  ...
}

Expected behavior

This error should not be a thing.

Environment

  • Pyodide Version:

Additional context

Four solutions are proposed through the discussion of the issues (link provided above):
A) using skipLibCheck: true in the tsconfig.json
B) as module owners, provide two different modules, one compiled for dom, one compiled for webworker
C) a new type from typescript, combining both dom and webworker as best as possible
D) users somehow writing new d.ts files to account for missing types (like HTMLCanvasElement)

A is not really a solution though because you usually do want your libraries to me checked.

B is something that you guys could consider doing.

C would be a solution that typescript maintainers could envision.

D is very unclear, at least to me because I'm not sure how to write types for incompatible code in npm packages. Somehow, when checking code, the installed packages in node_modules would have to consider those types.

Another proposed solution by me is to remove the HTMLCanvasElement from the pyodide.d.ts file, as it seems to be the only type that is in the "dom", but not in the "webworker" typings of typescript.

@Hephaistos7 Hephaistos7 added the bug Something isn't working label May 11, 2024
@hoodmane
Copy link
Member

Thanks for the report @Hephaistos7. I think you can work around it on your side by adding something like this:

declare global {
  export interface HTMLCanvasElement {}
}

@Hephaistos7
Copy link
Author

Yeah, that does it. Thank you.

I'll let you close the issue. You might want to do something else from your side.

@hoodmane
Copy link
Member

Let's keep it open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants