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

Literal types #296

Open
MasseGuillaume opened this issue Aug 23, 2017 · 7 comments
Open

Literal types #296

MasseGuillaume opened this issue Aug 23, 2017 · 7 comments

Comments

@MasseGuillaume
Copy link
Contributor

I find myself doing this a lot:

dom.document.createElement("div").asInstanceOf[dom.raw.HTMLDivElement]

It would make sense to have extensions for most of the dom.raw package.

@ritschwumm
Copy link

i have code for this lying around somewhere.

way too soon afterwards though, code like this began annoying me:

import tags._
val x = div()
x.id = "foo"
x.className = "message"
x.textContent = "hello"

so that preceived gain was not as big as i imagined.

@Grogs
Copy link
Contributor

Grogs commented Nov 13, 2017

If you import org.scalajs.dom.html._ then there are shorter type aliases for most elements. You example would become dom.document.createElement("div").asInstanceOf[Div].

I just live with that, although I'm not sure how users would know to import that package.

@Grogs
Copy link
Contributor

Grogs commented Nov 13, 2017

Thinking about it, in this case, it would be nice to be able to do: document.createElement[Div]. HTMLDivElement doesn't currently know the corresponding element name is 'div' though.

@ritschwumm
Copy link

that could easily be done with a simple type class

@ramnivas
Copy link
Contributor

ramnivas commented Feb 7, 2020

Another approach: https://blog.ramnivas.com/technology/2020/02/05/literal-types-to-simplify-code.html.

@raquo
Copy link
Contributor

raquo commented Feb 7, 2020

Wow, the literal types approach is super clean:

def createElement(tagName: "script"): HTMLScriptElement = js.native
def createElement(tagName: "a"): HTMLLinkElement = js.native
...

I understand why this repo wouldn't want custom createElementX methods – those wouldn't match the underlying JS API and would require an implementation. But it seems that literal types address this concern, the API would match the JS API, and would not require any implementation to be written, just the interfaces.

These new methods would only be available in 2.13+, but maybe that's good enough?

@armanbilge armanbilge changed the title Extension proposition dom.document.createElementX Literal types Aug 11, 2021
@armanbilge
Copy link
Member

I like the idea of literal types too, let's keep this on our radar :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants