Rayrun
← Back to Discord Forum

typescript issues when a fixture extends a fixture

wickynilliamsposted in #help-playwright
Open in Discord
wickynilliams
wickynilliams

hi all, i have a base fixture which configures some global bits like an authenticated user. in some cases i want to extend that base fixture to test some edge cases, e.g. an unautheticated user. consider the following

import { test as base } from '@playwright/test'

type BaseFixture = {
  user: { firstName: string; lastName: string }
}

const myFixture = base.extend<BaseFixture>({
  user: {
    firstName: 'Nick',
    lastName: 'Williams',
  },
})

i would then extend my fixture like so:

const myOtherFixture = myFixture.extend({
   // Type '{ firstName: string; lastName: string; }' is not assignable to type 'undefined'
  user: {
    firstName: 'John',
    lastName: 'Doe',
  }
})

the problem is that i get a confusing typescript error as shown above. after some trial and error i notice that i can make this error go away by explicitly passing the generic type parameter to extend:

const myOtherFixture = myFixture.extend<BaseFixture>({
  user: {
    firstName: 'John',
    lastName: 'Doe',
  }
})

am i doing something wrong to trigger the TS error? is this a bug in playwright? or is it expected that you always explicitly pass the type param (in which case, should it be made non-optional)?

This thread is trying to answer question "No information provided"

7 replies
wickynilliams
wickynilliams

typescript issues when a fixture extends a fixture

tphillips8117

Is there anything wrong with passing in the BaseFixture type parameter? I am relatively new to Typescript so I have experienced the same behaviour and wondered as well

wickynilliams
wickynilliams

nothing wrong per se. but i would hope for either a better error message (why does TS think the type is undefined?) or that the type parameter be made mandatory

wickynilliams
wickynilliams

maybe i will raise an issue on github. if i'm not the only one hitting this issue, it's probably worth a more formal discussion

tphillips8117

Answer from ChatGPT:

image.png
tphillips8117

I think the Typescript compiler has trouble inferring the type of myFixture when extending it

tphillips8117

so explicitly specifying it when calling .extend tells the compiler what type it is

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.