Rayrun
← Back to Discord Forum

Probably basic TypeScript question but why am I seeing an error about baseURL when the test passes?

fieldfare_24977posted in #help-playwright
Open in Discord
fieldfare_24977
fieldfare_24977

Hello! Anyone know why I am getting the following message from this code please?

Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.ts(2345)

and my actual test run works, it navigates to the baseURL and the test passes! So I don't understand why that error? Using VS Code. Also a total noob to TypeScript, Node, VSCode and Playwright, sorry!

image.png

This thread is trying to answer question "Why am I getting a TypeScript error about baseURL being 'string | undefined' when my test passes?"

7 replies

kinda self explanatory, not sure how to explain it in other words

baseURL is either a string or undefined and goto accepts only a string

fieldfare_24977
fieldfare_24977

Ahhhhhhhhhhh yes that does kinda make sense, so what can I do to actually use baseURL as a string only?

fieldfare_24977
fieldfare_24977

cast?

fieldfare_24977
fieldfare_24977

The concept of an undefined type is a bit alien to me sorry. If I have to set a string const for baseURL then I am going to have to either set it on every single page, or create a test base file somehow (no idea how to do this with TS yet LOL)

fieldfare_24977
fieldfare_24977

Stackoverflow just answered this for me and it makes more sense now, it's like having a string which could be null. OK got it, thanks!

When a baseURL via the context options was provided and the passed URL is a path, it gets merged via the new URL() constructor. You only need to .goto('')

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 [email protected].