-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
its
type should exclude null
and undefined
#28872
Comments
…ress-io#28872) This fix addresses an issue where the 'its' function could return null or undefined, causing unexpected behavior in certain scenarios. The change introduces a more robust type check to ensure that the returned value is always non-null and non-undefined.
@eostrom-livefront Yes, your proposed fix makes sense and is a reasonable solution to address the TypeScript error. By changing the return type of its to Chainable<NonNullable<Subject[K]>>, you are explicitly stating that the property accessed with its will not be null or undefined. This aligns with the behavior documented in Cypress, where it automatically retries until it has a property that is not null or undefined. |
…#28904) * refactor: ensure 'its' function type excludes null and undefined (#28872) This fix addresses an issue where the 'its' function could return null or undefined, causing unexpected behavior in certain scenarios. The change introduces a more robust type check to ensure that the returned value is always non-null and non-undefined. * resolve linting issues related to TypeScript types * Update changelog * resolved lint errors * Update cli/types/cypress.d.ts Co-authored-by: Ryan Manuel <ryanm@cypress.io> * Update cli/types/cypress.d.ts * move to correct changelog entry --------- Co-authored-by: Jennifer Shehane <jennifer@cypress.io> Co-authored-by: Ryan Manuel <ryanm@cypress.io>
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Current behavior
A Cypress test that relies on
its
to yield a value that is notnull
orundefined
will be flagged as an error in TypeScript, even though the docs assert that ".its()
will automatically retry until it has a property that is notnull
orundefined
."Desired behavior
Tests shouldn't have to handle values that can't occur.
Test code to reproduce
This test runs (and passes, for what it's worth). But TypeScript reports:
In this example, I can work around it by logging
frameElement?.id || ''
, but it would be better not to have to. And although workarounds are probably always possible, they're not always that simple.(My actual code involves a third-party analytics library that adds a property to
window
with an object I want to stub, but the property isn't available immediately on page load, so I have to wait for it. In JavaScript,its
is great for that! But I'm converting the tests to TypeScript, and TypeScript doesn't know it's great in that way.)Cypress Version
13.6.4
Node version
18.17.0
Operating System
MacOS 14.2.1
Debug Logs
No response
Other
It looks to me like this can be fixed by making the resulting
Chainable
subject non-nullable – changing:to:
I did that as a local override, and my TypeScript errors went away. That may or may not be the best solution.
The text was updated successfully, but these errors were encountered: