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

Image element src property does not allow URL assignment #43066

Open
TimvdLippe opened this issue Mar 3, 2021 · 6 comments
Open

Image element src property does not allow URL assignment #43066

TimvdLippe opened this issue Mar 3, 2021 · 6 comments
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript

Comments

@TimvdLippe
Copy link
Contributor

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)

@TimvdLippe
Copy link
Contributor Author

TimvdLippe commented Mar 3, 2021

Previous occurrences of this kind of issue:

At this point, should we assume that USVString implies string | URL? The handling of USVString is 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 use USVString, but wouldn't accept a URL (which would be implicitly .toString'd per the linked HTML issue)

CC @domenic do you mind confirming if we can assume type USVString = string | URL in TypeScript or am I still misunderstanding the terminology here πŸ˜„

@domenic
Copy link

domenic commented 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
Copy link
Contributor Author

Ah right. Yes I understand that now. Thanks again for the explanation!

@DanielRosenwasser
Copy link
Member

You want separate types for reading and writing properties, which @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.ts needed to change and punted on it, but this seems like a reasonable one (write of URL | string, read of string).

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 4, 2021

So hard-coding URL doesn't make sense to me.

We'd want to be more permissive to a degree; letting anything in with a toString() seems undesirable, whereas unblocking a scenario like assigning URL instances to a property called url the src property seems reasonable.

@RyanCavanaugh
Copy link
Member

Tracking at #2521. We can update the lib once that's checked in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants