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

Assigning window.location after 5.8.2 upgrade fails #61335

Open
AndreasPresthammer opened this issue Mar 3, 2025 · 3 comments
Open

Assigning window.location after 5.8.2 upgrade fails #61335

AndreasPresthammer opened this issue Mar 3, 2025 · 3 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@AndreasPresthammer
Copy link

AndreasPresthammer commented Mar 3, 2025

🔎 Search Terms

Unable to assign window.location typescript 5.8.2
error TS2322: Type 'Location' is not assignable to type 'string & Location'.
Type 'Location' is not assignable to type 'string'.

🕗 Version & Regression Information

  • This changed between versions 5.7.3 and 5.8.2

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.2#code/O4SwdgJg9sB0A2UDGBDALiKYAEBebokMCy6mYA3AFBA

💻 Code

window.location = window.location;

🙁 Actual behavior

Failing with error:

error TS2322: Type 'Location' is not assignable to type 'string & Location'.
  Type 'Location' is not assignable to type 'string'.

🙂 Expected behavior

Expect it to either be a documented breaking change, OR it should continue to work as it did in 5.7.3:

https://www.typescriptlang.org/play/?ts=5.7.3#code/O4SwdgJg9sB0A2UDGBDALiKYAEBebokMCy6mYA3AFBA

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Mar 3, 2025
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.8.0 milestone Mar 3, 2025
@MartinJohns
Copy link
Contributor

Related: #48949

@chrisvltn
Copy link

chrisvltn commented Mar 4, 2025

Had the same issue when upgrading to the new version.

A similar workaround to this one worked for me:

const win: typeof globalThis = window;
win.location = window.location; // Pass

I see the PR #60987 added this part:

Document.location and Window.location now has stricter get/set types:

  • get location(): Location is unchanged
  • set location(href: string) previously allowed Location (probably because of Typescript's accessor assignability rules)

Seems to be the cause of the change, as set location expects a string, but globalThis['location'] expects a Location

@reifi
Copy link

reifi commented Mar 5, 2025

A pain for old style iframe prevention - ts-ignore helps

if (window.top !== window.self) {
  if (window.top) {  
    // @ts-ignore
    window.top.location = window.self.location;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

6 participants