Skip to content

Commit 42a5566

Browse files
authored
fix(types): fix waitForSelector typing to not union null when appropriate (#6344)
Previously when options were defined, but no `state` key was provided, the types would return null as an option. Even though the default state is `visible` and shouldn't allow `null`. Tests updated to fail appropriately and new tests added for this case.
1 parent 8d66edf commit 42a5566

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

types/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import { Readable } from 'stream';
2121
import { Serializable, EvaluationArgument, PageFunction, PageFunctionOn, SmartHandle, ElementHandleForTag, BindingSource } from './structs';
2222

2323
type PageWaitForSelectorOptionsNotHidden = PageWaitForSelectorOptions & {
24-
state: 'visible'|'attached';
24+
state?: 'visible'|'attached';
2525
};
2626
type ElementHandleWaitForSelectorOptionsNotHidden = ElementHandleWaitForSelectorOptions & {
27-
state: 'visible'|'attached';
27+
state?: 'visible'|'attached';
2828
};
2929

3030
/**

utils/generate_types/overrides.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import { Readable } from 'stream';
2020
import { Serializable, EvaluationArgument, PageFunction, PageFunctionOn, SmartHandle, ElementHandleForTag, BindingSource } from './structs';
2121

2222
type PageWaitForSelectorOptionsNotHidden = PageWaitForSelectorOptions & {
23-
state: 'visible'|'attached';
23+
state?: 'visible'|'attached';
2424
};
2525
type ElementHandleWaitForSelectorOptionsNotHidden = ElementHandleWaitForSelectorOptions & {
26-
state: 'visible'|'attached';
26+
state?: 'visible'|'attached';
2727
};
2828

2929
export interface Page {

utils/generate_types/test/test.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -675,47 +675,58 @@ playwright.chromium.launch().then(async browser => {
675675
}
676676
}
677677

678+
type AssertCanBeNull<T> = null extends T ? true : false
679+
678680
const frameLikes = [page, frame];
679681
for (const frameLike of frameLikes) {
680682
{
681683
const handle = await frameLike.waitForSelector('body');
684+
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
685+
const canBeNull: AssertCanBeNull<typeof handle> = false
686+
}
687+
{
688+
const handle = await frameLike.waitForSelector('body', {timeout: 0});
682689
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
683-
const canBeNull: AssertType<null, typeof handle> = false;
690+
const canBeNull: AssertCanBeNull<typeof handle> = false;
684691
}
685692
{
686693
const state = Math.random() > .5 ? 'attached' : 'visible';
687694
const handle = await frameLike.waitForSelector('body', {state});
688695
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
689-
const canBeNull: AssertType<null, typeof handle> = false;
696+
const canBeNull: AssertCanBeNull<typeof handle> = false;
690697
}
691698
{
692699
const handle = await frameLike.waitForSelector('body', {state: 'hidden'});
693700
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
694-
const canBeNull: AssertType<null, typeof handle> = true;
701+
const canBeNull: AssertCanBeNull<typeof handle> = true;
695702
}
696703
{
697704
const state = Math.random() > .5 ? 'hidden' : 'visible';
698705
const handle = await frameLike.waitForSelector('body', {state});
699706
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
700-
const canBeNull: AssertType<null, typeof handle> = true;
707+
const canBeNull: AssertCanBeNull<typeof handle> = true;
701708
}
702-
703709
{
704710
const handle = await frameLike.waitForSelector('something-strange');
705711
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
706-
const canBeNull: AssertType<null, typeof handle> = false;
712+
const canBeNull: AssertCanBeNull<typeof handle> = false;
707713
}
714+
{
715+
const handle = await frameLike.waitForSelector('something-strange', {timeout: 0});
716+
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
717+
const canBeNull: AssertCanBeNull<typeof handle> = false;
718+
}
708719
{
709720
const state = Math.random() > .5 ? 'attached' : 'visible';
710721
const handle = await frameLike.waitForSelector('something-strange', {state});
711722
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
712-
const canBeNull: AssertType<null, typeof handle> = false;
723+
const canBeNull: AssertCanBeNull<typeof handle> = false;
713724
}
714725
{
715726
const state = Math.random() > .5 ? 'hidden' : 'visible';
716727
const handle = await frameLike.waitForSelector('something-strange', {state});
717728
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
718-
const canBeNull: AssertType<null, typeof handle> = true;
729+
const canBeNull: AssertCanBeNull<typeof handle> = true;
719730
}
720731
}
721732

0 commit comments

Comments
 (0)