Skip to content
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

fix(types): fix waitForSelector typing to not union null when appropriate #6344

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import { Readable } from 'stream';
import { Serializable, EvaluationArgument, PageFunction, PageFunctionOn, SmartHandle, ElementHandleForTag, BindingSource } from './structs';

type PageWaitForSelectorOptionsNotHidden = PageWaitForSelectorOptions & {
state: 'visible'|'attached';
state?: 'visible'|'attached';
};
type ElementHandleWaitForSelectorOptionsNotHidden = ElementHandleWaitForSelectorOptions & {
state: 'visible'|'attached';
state?: 'visible'|'attached';
};

/**
Expand Down
4 changes: 2 additions & 2 deletions utils/generate_types/overrides.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import { Readable } from 'stream';
import { Serializable, EvaluationArgument, PageFunction, PageFunctionOn, SmartHandle, ElementHandleForTag, BindingSource } from './structs';

type PageWaitForSelectorOptionsNotHidden = PageWaitForSelectorOptions & {
state: 'visible'|'attached';
state?: 'visible'|'attached';
};
type ElementHandleWaitForSelectorOptionsNotHidden = ElementHandleWaitForSelectorOptions & {
state: 'visible'|'attached';
state?: 'visible'|'attached';
};

export interface Page {
Expand Down
27 changes: 19 additions & 8 deletions utils/generate_types/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,47 +675,58 @@ playwright.chromium.launch().then(async browser => {
}
}

type AssertCanBeNull<T> = null extends T ? true : false

const frameLikes = [page, frame];
for (const frameLike of frameLikes) {
{
const handle = await frameLike.waitForSelector('body');
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
const canBeNull: AssertCanBeNull<typeof handle> = false
}
{
const handle = await frameLike.waitForSelector('body', {timeout: 0});
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
const canBeNull: AssertType<null, typeof handle> = false;
const canBeNull: AssertCanBeNull<typeof handle> = false;
}
{
const state = Math.random() > .5 ? 'attached' : 'visible';
const handle = await frameLike.waitForSelector('body', {state});
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
const canBeNull: AssertType<null, typeof handle> = false;
const canBeNull: AssertCanBeNull<typeof handle> = false;
}
{
const handle = await frameLike.waitForSelector('body', {state: 'hidden'});
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
const canBeNull: AssertType<null, typeof handle> = true;
const canBeNull: AssertCanBeNull<typeof handle> = true;
}
{
const state = Math.random() > .5 ? 'hidden' : 'visible';
const handle = await frameLike.waitForSelector('body', {state});
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
const canBeNull: AssertType<null, typeof handle> = true;
const canBeNull: AssertCanBeNull<typeof handle> = true;
}

{
const handle = await frameLike.waitForSelector('something-strange');
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
const canBeNull: AssertType<null, typeof handle> = false;
const canBeNull: AssertCanBeNull<typeof handle> = false;
}
{
const handle = await frameLike.waitForSelector('something-strange', {timeout: 0});
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
const canBeNull: AssertCanBeNull<typeof handle> = false;
}
{
const state = Math.random() > .5 ? 'attached' : 'visible';
const handle = await frameLike.waitForSelector('something-strange', {state});
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
const canBeNull: AssertType<null, typeof handle> = false;
const canBeNull: AssertCanBeNull<typeof handle> = false;
}
{
const state = Math.random() > .5 ? 'hidden' : 'visible';
const handle = await frameLike.waitForSelector('something-strange', {state});
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
const canBeNull: AssertType<null, typeof handle> = true;
const canBeNull: AssertCanBeNull<typeof handle> = true;
}
}

Expand Down