Anyone has any idea how to write an external for s...
# javascript
t
Anyone has any idea how to write an external for such declaration (WebIDL):
Copy code
enum DOMParserSupportedType {
  "text/html",
  "text/xml",
  "application/xml",
  "application/xhtml+xml",
  "image/svg+xml"
};
😅
b
How is that declared on js side?
m
I think it’s just a string. So the legit way would be something like this:
Copy code
external interface DOMParserSupportedType

object DOMParserSupportedTypeStatic {

    inline val text_html get() = "text/html".unsafeCast<DOMParserSupportedType>()
    inline val text_xml get() = "text/xml".unsafeCast<DOMParserSupportedType>()
    inline val application_xml get() = "application/xml".unsafeCast<DOMParserSupportedType>()
    inline val application_xhtml_xml get() = "application/xhtml+xml".unsafeCast<DOMParserSupportedType>()
    inline val image_svg_xml get() = "image/svg+xml".unsafeCast<DOMParserSupportedType>()
}
And nicer usage but quite unsafe:
Copy code
external interface DOMParserSupportedType {

    @Suppress(
        "INLINE_EXTERNAL_DECLARATION",
        "NAME_CONTAINS_ILLEGAL_CHARS",
        "NESTED_CLASS_IN_EXTERNAL_INTERFACE",
        "ObjectPropertyName",
        "WRONG_BODY_OF_EXTERNAL_DECLARATION",
    )
    companion object {

        inline val `text/html` get() = "text/html".unsafeCast<DOMParserSupportedType>()
        inline val `text/xml` get() = "text/xml".unsafeCast<DOMParserSupportedType>()
        inline val `application/xml` get() = "application/xml".unsafeCast<DOMParserSupportedType>()
        inline val `application/xhtml+xml` get() = "application/xhtml+xml".unsafeCast<DOMParserSupportedType>()
        inline val `image/svg+xml` get() = "image/svg+xml".unsafeCast<DOMParserSupportedType>()
    }
}
b
If it's just a string, why make it external?
Copy code
typealias DOMParserSupportedType = String

inline val DOMParserSupportedType.text_html: DOMParserSupportedType get() = "text/html"
// others
m
Because
typealias
drops type safety
val x: DOMParserSupportedType = "foo"
b
In what way? String is a String
m
It’s an enum. It’s just represented as a string.
t
@Marc Knaup That's probably fine, since js enums are just strings.
b
Ah, you're right
Looks like the best approach is this:
Copy code
external interface DOMParserSupportedType {
  companion object
}
inline val DOMParserSupportedType.Companion.text_html get() = "text/html".unsafeCast<DOMParserSupportedType>()
// others
t
TS declarations can be found in - https://github.com/microsoft/TypeScript/blob/master/src/lib/dom.generated.d.ts Specification can be found - https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html I'm actually not writing this code by hand. I'm working on WebIDL -> Kotlin compiler
m
@Big Chungus a
companion object
within an
external interface
is not legit. At least not in IR.
@Tomasz Krakowiak you can use Dukat to automatically create external definitions from TypeScript definitions. See what it comes up with 🙂
b
Then this "might" (emphasis on might) work
Copy code
external interface DOMParserSupportedType
inline val DOMParserSupportedType.Companion.text_html get() = "text/html".unsafeCast<DOMParserSupportedType>()
// others
m
@Big Chungus no, because there is no Companion.
Okay, Dukat doesn’t work. Kotlin’s stdlib already has
DOMParser
definitions and simply uses
dynamic
for
DOMParserSupportedType
😮
t
Ok. Thanks everyone : ) I'll try Dukat. I think it supports WebIDL as well : ) (with at least one exception I support ; p)
m
image.png
t
And following works in IR with suppresses https://pl.kotl.in/mCQRt3SfR
Screen Shot 2020-11-27 at 9.59.59 AM.png
If you need external interface companions - you can vote here
t
Tried to go with something like - https://pl.kotl.in/d6LfPhnBD But I got some duplicated declarations. (NOTE: I am also looking for server-side support of reasonable portion of web APIs)