fix(types): Fix overwriteCommand inconsistent typing#14549
fix(types): Fix overwriteCommand inconsistent typing#14549christian-bromann merged 11 commits intowebdriverio:mainfrom
overwriteCommand inconsistent typing#14549Conversation
- Fix inference having the `this` which was in excess - Fix inference wrapping in a `(...arg) =>` which was in excess - Fix browser command overwrite where the return promise type is wrong because undefined verbose return type is inferred to `Promise<unknown>`
overwriteCommand bad typing
eslint-plugin-wdio
@wdio/allure-reporter
@wdio/appium-service
@wdio/browser-runner
@wdio/browserstack-service
@wdio/cli
@wdio/concise-reporter
@wdio/config
@wdio/cucumber-framework
@wdio/dot-reporter
@wdio/firefox-profile-service
@wdio/globals
@wdio/jasmine-framework
@wdio/json-reporter
@wdio/junit-reporter
@wdio/lighthouse-service
@wdio/local-runner
@wdio/logger
@wdio/mocha-framework
@wdio/protocols
@wdio/repl
@wdio/reporter
@wdio/runner
@wdio/sauce-service
@wdio/shared-store-service
@wdio/smoke-test-cjs-service
@wdio/smoke-test-reporter
@wdio/smoke-test-service
@wdio/spec-reporter
@wdio/static-server-service
@wdio/sumologic-reporter
@wdio/testingbot-service
@wdio/types
@wdio/utils
@wdio/webdriver-mock-service
webdriver
webdriverio
commit: |
overwriteCommand bad typingoverwriteCommand inconsistent typing
I think this makes sense to me. We should deprecate the |
christian-bromann
left a comment
There was a problem hiding this comment.
Great cleanup 👏 let's address above comment and get this merged.
Trying in the following days... |
|
I will open another PR since I'm getting some challenges, so that we can merge the fixes! I reverted the proposition from the PR! |
|
Hey dprevost-LMI 👋 Thank you for your contribution to WebdriverIO! Your pull request has been marked as an "Expensable" contribution. We've sent you an email with further instructions on how to claim your expenses from our development fund. We are looking forward to more contributions from you in the future 🙌 Have a nice day, |
About the information above, I gave it a try, but I have some doubts, unfortunately.
What would make more sense:
So, as a prototyping start, it would look like the below (utopian thinking since we are currently forgetting for a minute that we need to support a deprecated API) type BrowserTypeInstances = WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser
export interface BrowserInstanceCommands<T extends BrowserTypeInstances> {
/**
* Add a command to `browser`
*/
addCommand (
name: string,
func: AddCommandFn,
proto?: Record<string, any>,
instances?: Record<string, T>
): void;
/**
* Add a command to elements
*/
addElementCommand<Instance extends BrowserTypeInstances = WebdriverIO.Browser>(
name: string,
func: AddCommandFnScoped<T | Instance, true>,
// Unsure if needed or not, maybe just internally needed if so we could separate from public API
// proto?: Record<string, any>,
// instances?: Record<string, Instances>
): void;
/**
* Overwrite a `browser` command
*/
overwriteCommand<ElementKey extends keyof $ElementCommands, BrowserKey extends keyof $BrowserCommands>(
name: BrowserKey,
func: OverwriteCommandFn<ElementKey, BrowserKey, false>,
proto?: Record<string, any>,
instances?: Record<string, BrowserTypeInstances>
): void;
/**
* Overwrite an element command
*/
overwriteElementCommand<ElementKey extends keyof $ElementCommands, BrowserKey extends keyof $BrowserCommands>(
name: BrowserKey,
func: OverwriteCommandFnScoped<ElementKey, BrowserKey, true>,
// Unsure if needed or not, maybe just internally needed if so we could separate from public API
// proto?: Record<string, any>,
// instances?: Record<string, BrowserTypeInstances>
): void;
/**
* Create a custom selector
*/
addLocatorStrategy(
name: string,
func: ((selector: string, root: HTMLElement) => CustomLocatorReturnValue) | ((selector: string) => CustomLocatorReturnValue)
): void
}
export interface ElementInstanceCommands<T extends WebdriverIO.Element> {
/**
* add command to `browser` or `element` scope
*/
addCommand<IsElement extends boolean = false, Instance extends BrowserTypeInstances = WebdriverIO.Browser>(
name: string,
func: AddCommandFnScoped<T | Instance, IsElement>,
): void;
// Needed?? Probably not...
// overwriteCommand(): void;
// addLocatorStrategy(): void
}Seeing the complexity, either I just won't do it, or from now on, it will need its dedicated issue, so we can stamp all this properly instead of hijacking this issue 😅 |
|
@dprevost-LMI thanks for the explanation. I agree with you. I have two options in my mind:
Let's create a new issue and get more feedback from the community. |
Proposed changes
As exposed in this issue, when using
overwriteCommand, the typing is not always correctly inferred. We attempt to fix a couple of them and more:Things done:
thisrefer toWebdriverIO.Elementautomatically in thefuncofoverwriteCommand&addCommandthisinference to Browser automatically in thefuncofoverwriteCommand&addCommand(overkill?)originalCommandFunctioninfer the correct return type by adding the return type explicitly in existing browser commandsthisin existing examples.overwriteBrowserCommandandoverwriteElementCommand, instead of using the boolean, to ease the API and not worry about missing the boolean or not. Will also reduce the typing of definitionsaddBrowserCommandandaddElementCommand, instead of using the boolean, to ease the API and not worry about missing the boolean or not. Will also reduce the typing of definitionsTypes of changes
Checklist
Backport Request
//: # (The current
mainbranch is the development branch for WebdriverIO v9. If your change should be released to the current major version of WebdriverIO (v8), please raise another PR with the same changes against thev8branch.)v9and doesn't need to be back-ported#XXXXXFurther comments
Please let me know about the new API proposal and whether you would like to deprecate the former or keep it.
Reviewers: @webdriverio/project-committers