question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'window.location' should allow assigning a string

See original GitHub issue

Bug Report

🔎 Search Terms

window.location, location.href

🕗 Version & Regression Information

  • No version has the behaviour I’d expect, however up to version 4.5.5 windows.location is of type Location, while from version 4.6.2 is of type (string | Location) & Location. FAQ do not mention anything about it.

⏯ Playground Link

Playground link with relevant code

💻 Code

window.location = "https://github.com/";

🙁 Actual behavior

window.location = "https://github.com/";
// (property) location: (string | Location) & Location
// Type 'string' is not assignable to type '(string | Location) & Location'.(2322)

While it’s true that Location is an object and not a string, assigning a string to it is a long-established and recommended pattern, that may produce a different result from window.location.href = "https://github.com/";.

Example of where assigning a string to window.location is the correct behaviour: https://javascript.info/cross-window-communication#4q5rssu5ys

Also, according to MDN:

Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with location as if it were a string in most cases: location = 'http://www.example.com' is a synonym of location.href = 'http://www.example.com'.

Further discussion on difference between assigning to location or to location.href: https://stackoverflow.com/questions/2383401/javascript-setting-location-href-versus-location

🙂 Expected behavior

window.location = "https://github.com/";
// (property) location: string | Location
// No error

Mock playground

I could not find the rationale behind the change that happened between 4.5.5 and 4.6.2, that changed its type from Location to (string | Location) & Location, but I wonder if the intended result was the same I’m expecting here.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
KKrisucommented, Aug 3, 2022

(window as Window).location = 'abc' ✔️

14reactions
el1s7commented, Sep 20, 2022

Typescript is full of annying shit like this, just do

//@ts-ignore
window.location = '/'
Read more comments on GitHub >

github_iconTop Results From Across the Web

Set window.location with TypeScript - javascript - Stack Overflow
window.location is of type Location while .attr('data-href') returns a string, so you have to assign it to window.location.href which is of ...
Read more >
location.assign() - Web APIs - MDN Web Docs
The Location.assign() method causes the window to load and display the document at the URL specified. After the navigation occurs, ...
Read more >
window.location Cheatsheet | SamanthaMing.com
The window.location object can be used to get information on the current page address (URL). You can also use its method to do...
Read more >
Location assign() Method - W3Schools
Window location.assign() · Definition and Usage · See Also: · Note · Syntax · Parameters · Return Value · Browser Support · Report...
Read more >
Difference between window.location.href ... - GeeksforGeeks
The assign() method is also preferred over href as it allows the user to mock the function and check the URL input parameters...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found