-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[BUG] typing files and real code are different #3978
Comments
The short answer is that we don't export classes as part of our public api, so this technique will not work with playwright. This is unrelated to our manual typings. We've had good reasons for not exporting classes. All of them require a private async initialization step, so the constructors are meaningless. On multiple times we've switched things from being a class to an interface with multiple implementations. If we had exported classes, these would have been breaking changes. TypeScript metadata is an interesting new argument in favor of exporting classes, but by itself I don't think its worth it. The browser class constructor doesn't contain enough information to properly construct the browser object. You are going to have to manually write the construction code, and then you might as well just wrap it in a function that asynchronously returns a singleton. If you have some context as to the root problems you are trying to solve, I think the https://github.com/microsoft/playwright-test project would be interested in solving them with fixtures. |
Closing as wontfix, but feel free to continue the discussion here or in playwright-test. |
Yes, and it is ok. The main idea of the If you don't want users to call the constructor - you can declare it as Manual typings are the distortion of reality. There are at least two signs that this is the wrong approach:
This is uncertainty, which is inherently bad. I can understand that you are trying to protect users from the mistakes, but, for real, we are not kids, we are developers (sometimes it's the same person, but... 😄 ). If i use the class (even if this class was returned from |
i really hope we could re-open this issue |
It will make life easier not only for me as a user but for you as maintainers. Because having manually written typing files while you have original TypeScript code is a violation of the universal Single source of truth practice. Every time you make 1 change you should make it in 2 places.
Why? Nothing will break if you will export something, that you have not exported before. |
You are telling
But your documentation declares https://playwright.dev/#version=v1.4.2&path=docs%2Fapi.md&q=class-browser If it is not a public API, then what is it? |
Another shortcoming is the impossibility to check the concrete browser instance. For example, i accept the abstract export class MyModule {
constructor(browser: Browser) {
// Impossible to check
if(brower instanceof ChromiumBrowser) {
doSomethingSpecial()
}
}
} @pavelfeldman @dgozman please, pay attention to this issue. |
I know this is not what you are asking for, but we can introduce On the topic of exporting the classes: are there any other usecases except for using them as DI tokens? I'd like to understand pros vs cons of exposing the classes. Perhaps, there are more folks interested in using our classes for DI? |
Yes, but... why to do this workaround while JavaScript offers a standard way to check if an object is an instance of some class - there is a special const browser = await firefox.launch();
// Looks strange, like tradeoff
if(browser.name === 'firefox') {}
// Reads like speech - if browser instance of firefox
if(browser instanceof FirefoxBrowser) {} I think it is obvious that we should utilize the native language features instead of intent our own. Moreover, i would understand if you have some reasons to search for a compromise workaround, but you already using real human classes which we can use like we are bosses. So i don't understand why reinvent the wheel. So, for now, i have 3 main arguments in favor of exporting classes:
I already described these arguments in detail. I really don't understand your reasons to hide the classes. |
i mention 5 cons
Can someone tell any cons? |
Guys 😢 ? |
Is there any updates? |
Context:
It is not really a bug, but it is a problem. You are using TypeScript. Me too. But it looks like you are writing the
types.d.ts
manually, and it is different from the actual code. You are using classes, for example - browser. But in yourtypes.d.ts
it is declared asinterface Browser
.The main issue - as i mentioned, i use TypeScript, and i use the
Dependency Injection
technique. It is very convenient when you declare it like thisi hope you know how TypeScript reflection works, and understand that if
Browser
is declared asinterface
- the reflection doesn't work, so i am forced to use workarounds.Your sources are in TypeScript, so why not just let
tsc
generate proper typings automatically?The text was updated successfully, but these errors were encountered: