Image element src property does not allow URL assignment #43066
Copy link
Copy link
Open
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Bug Report
🔎 Search Terms
image src dom
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about 4.3.0
⏯ Playground Link
Playground link with relevant code
💻 Code
let image = document.createElement('img');
image.src = new URL("http://google.com");
console.log(image.src)🙁 Actual behavior
Type 'URL' is not assignable to type 'string'.(2322)
🙂 Expected behavior
Compiles cleanly (confirmed supported per whatwg/html#6440)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
TimvdLippe commentedon Mar 3, 2021
Previous occurrences of this kind of issue:
At this point, should we assume that
USVStringimpliesstring | URL? The handling ofUSVStringis currently implemented in https://github.com/microsoft/TypeScript-DOM-lib-generator/blob/07ea4e8bf58f9907d22024cca6433e002fffd251/src/helpers.ts#L6 I am not sure how many other IDL definitions useUSVString, but wouldn't accept aURL(which would be implicitly.toString'd per the linked HTML issue)CC Domenic Denicola (@domenic) do you mind confirming if we can assume
type USVString = string | URLin TypeScript or am I still misunderstanding the terminology here 😄domenic commentedon Mar 3, 2021
It's more subtle than that. Every DOMString or USVString will convert any input to a string. Technically, everything in JavaScript is convertible to a string, at the very least through Object.prototype.toString(). So hard-coding URL doesn't make sense to me.
TimvdLippe commentedon Mar 3, 2021
Ah right. Yes I understand that now. Thanks again for the explanation!
DanielRosenwasser commentedon Mar 4, 2021
You want separate types for reading and writing properties, which Ryan Cavanaugh (@RyanCavanaugh) is iterating on over at #42425.
I guess we did discuss a few things in a previous design meeting about how much of
lib.d.tsneeded to change and punted on it, but this seems like a reasonable one (write ofURL | string, read ofstring).DanielRosenwasser commentedon Mar 4, 2021
We'd want to be more permissive to a degree; letting anything in with a
toString()seems undesirable, whereas unblocking a scenario like assigningURLinstances toa property calledtheurlsrcproperty seems reasonable.RyanCavanaugh commentedon Mar 9, 2021
Tracking at #2521. We can update the lib once that's checked in