Skip to content

[Question] TypeScript compilation must include the browser environment #3037

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mightyiam opened this issue Jul 20, 2020 · 7 comments
Closed
Assignees

Comments

@mightyiam
Copy link

In order to get a successful TypeScript compilation, the dom types must be included (previously mentioned in #2659 (comment)).

Doing that results in TypeScript allowing access to browser globals such as window, document, URL and many more, which do not really exist in Node.js.

Is this recognized as an issue that would possibly be addressed, please?

@mxschmitt
Copy link
Member

How do you see there an issue? It's just the types which allow then the usage of the browser types in Node.js. But if you use them incorrectly it would result in an error thrown by Node.js.
Mostly it's useful for page.evaluate() e.g. and used for some internal scenarios.

@mightyiam
Copy link
Author

I use type checking to prevent runtime errors. Regarding page.evaluate, If you ask me, it should accept only a string. So I don't value this convenience. I value type checking much more. Having all the browser globals in the Node.js seems like a down-side.

@JoelEinbinder
Copy link
Contributor

We've seen a lot of value from having page.evaluate be typed, and doing so requires having the browser globals. Moving evaluation to be string only would be a huge change to the project, so I'm closing this as wontfix.

@mightyiam
Copy link
Author

@JoelEinbinder I am not for the parameter of page.evaluate to be a string only. If anything, I am asking to not have browser globals in my TypeScript Node.js project in order to use Playwright. Please do not close for a misunderstanding.

@paya-cz
Copy link

paya-cz commented May 14, 2021

I am having the same issue - in order to use DOM types inside of page.evaluate, I have to import DOM lib for the entire nodeJs project. The nodeJs project is doing a lot more things than just interface with playwright and having the global namespace polluted with DOM types, window, document and others is really not that great - imagine writing a server-side database client with access to these! This effectively causes TypeScript to ignore a bunch of clear compile-time errors until runtime.

Please resurrect this issue. No idea why the unrelated page.evaluate should accept string idea completely shadowed the issue described in the first post.

@davelosert
Copy link

I agree - this issue was closed too early. Playwright is a tool running in NodeJS - so why would I have to add all the browser types to my globals just for the use of some evaluating functions?

Please reconsider finding another solution here...

@DetachHead
Copy link
Contributor

i think microsoft/TypeScript#43434 would solve this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants