From c4fd612bc346dae4e09789b3af45d88219718af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 27 Nov 2024 14:42:00 +0100 Subject: [PATCH] fix(types): fixed `Fixtures` type to disambiguite between intersected mapped types --- packages/playwright/types/test.d.ts | 4 +- .../playwright-test-plugin-types.ts | 63 +++++++++++++++++++ utils/generate_types/overrides-test.d.ts | 4 +- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts index 8d05bdafef..4b01b80002 100644 --- a/packages/playwright/types/test.d.ts +++ b/packages/playwright/types/test.d.ts @@ -5663,9 +5663,9 @@ export type Fixtures | [TestFixtureValue, { scope: 'test', timeout?: number | undefined, title?: string, box?: boolean }]; } & { - [K in keyof W]?: [WorkerFixtureValue, { scope: 'worker', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean }]; + [K in keyof W as Exclude]?: [WorkerFixtureValue, { scope: 'worker', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean }]; } & { - [K in keyof T]?: TestFixtureValue | [TestFixtureValue, { scope?: 'test', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean }]; + [K in keyof T as Exclude]?: TestFixtureValue | [TestFixtureValue, { scope?: 'test', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean }]; }; type BrowserName = 'chromium' | 'firefox' | 'webkit'; diff --git a/tests/installation/fixture-scripts/playwright-test-plugin-types.ts b/tests/installation/fixture-scripts/playwright-test-plugin-types.ts index 880520ebce..b882595c82 100644 --- a/tests/installation/fixture-scripts/playwright-test-plugin-types.ts +++ b/tests/installation/fixture-scripts/playwright-test-plugin-types.ts @@ -17,3 +17,66 @@ test('sample test', async ({ page, plugin }) => { // @ts-expect-error await expect(page).toContainText(123); }); + +test1.extend({ + page: async ({ page }) => { + type IsPage = (typeof page) extends Page ? true : never; + const isPage: IsPage = true; + }, +}); + +test1.extend<{ myFixture: (arg: number) => void }>({ + page: async ({ page }) => { + type IsPage = (typeof page) extends Page ? true : never; + const isPage: IsPage = true; + }, +}); + +test1.extend({ + myFixture: async ({ page }) => { + type IsPage = (typeof page) extends Page ? true : never; + const isPage: IsPage = true; + } +}); + +test1.extend<{ myFixture: (arg: number) => void }>({ + myFixture: async ({ page }) => { + type IsPage = (typeof page) extends Page ? true : never; + const isPage: IsPage = true; + } +}); + +test1.extend({ + page: async ({ page }) => { + type IsPage = (typeof page) extends Page ? true : never; + const isPage: IsPage = true; + }, + myFixture: async ({ page }) => { + type IsPage = (typeof page) extends Page ? true : never; + const isPage: IsPage = true; + } +}); + +test1.extend<{ myFixture: (arg: number) => void }>({ + page: async ({ page }) => { + type IsPage = (typeof page) extends Page ? true : never; + const isPage: IsPage = true; + }, + myFixture: async ({ page }) => { + type IsPage = (typeof page) extends Page ? true : never; + const isPage: IsPage = true; + } +}); + +test1.extend<{ myFixture: (arg: number) => void }>({ + // @ts-expect-error + myFixture: (arg: number) => {}, +}); + +test1.extend<{ myFixture: (arg: number) => void }>({ + myFixture: async (_, use) => { + use((arg: number) => {}); + // @ts-expect-error + use((arg: string) => {}); + } +}); diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index 5775917382..f811fec269 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -178,9 +178,9 @@ export type Fixtures | [TestFixtureValue, { scope: 'test', timeout?: number | undefined, title?: string, box?: boolean }]; } & { - [K in keyof W]?: [WorkerFixtureValue, { scope: 'worker', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean }]; + [K in keyof W as Exclude]?: [WorkerFixtureValue, { scope: 'worker', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean }]; } & { - [K in keyof T]?: TestFixtureValue | [TestFixtureValue, { scope?: 'test', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean }]; + [K in keyof T as Exclude]?: TestFixtureValue | [TestFixtureValue, { scope?: 'test', auto?: boolean, option?: boolean, timeout?: number | undefined, title?: string, box?: boolean }]; }; type BrowserName = 'chromium' | 'firefox' | 'webkit';