You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define and use a custom locator in TypeScript code.
What do you get instead?
Custom locators cannot be added to the type definitions, leading to compiler errors when trying to use them in any methods with LocatorOrString parameters.
e2e/tests/xyz.ts:166:59 - error TS2322: Type 'string' is not assignable to type'(locator: LocatorOrString) => Locator'.
166 I.seeElement({ custom: "xyz", option: true });
Details
CodeceptJS version: 3.0.4
TypeScript version: 4.1.3
Proposed Solution
The way to add custom types from user code is declaration merging (as already done with the Methods interface). However, the types ILocator and LocatorOrString are fixed union types that cannot be extended. Therefore I suggest to add a CustomLocators interface that can be extended from outside and that will be used to build the LocatorOrString type.
The property keys used in the interface are arbitrary (only the property types will be used), but should reflect the keys used in the locator object. After that, it is possible to extend the CustomLocators interface:
declare global {namespaceCodeceptJS{interfaceCustomLocators{custom: {custom: string;option?: boolean};}}}
The text was updated successfully, but these errors were encountered:
danielrentz
added a commit
to danielrentz/CodeceptJS
that referenced
this issue
Feb 2, 2021
What are you trying to achieve?
Define and use a custom locator in TypeScript code.
What do you get instead?
Custom locators cannot be added to the type definitions, leading to compiler errors when trying to use them in any methods with
LocatorOrString
parameters.Details
Proposed Solution
The way to add custom types from user code is declaration merging (as already done with the
Methods
interface). However, the typesILocator
andLocatorOrString
are fixed union types that cannot be extended. Therefore I suggest to add aCustomLocators
interface that can be extended from outside and that will be used to build theLocatorOrString
type.Example:
The property keys used in the interface are arbitrary (only the property types will be used), but should reflect the keys used in the locator object. After that, it is possible to extend the
CustomLocators
interface:The text was updated successfully, but these errors were encountered: