'window.location' should allow assigning a string
See original GitHub issueBug 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 typeLocation
, 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-onlyLocation
object, you can also assign a DOMString to it. This means that you can work withlocation
as if it were astring
in most cases:location = 'http://www.example.com'
is a synonym oflocation.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
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:
- Created a year ago
- Comments:13 (4 by maintainers)
(window as Window).location = 'abc'
✔️Typescript is full of annying shit like this, just do