Emojis as variant

Is it possible to create a variant type of emojis?

Im trying to emulate a ts type that looks like this:

type Emojis = "πŸ˜€" | "πŸ˜ƒ" | "πŸ˜„" 

This doesnt seem to work.

Maybe there is somewhere to get a named list of emojis that we could build a variant with?

type emojis = [ \"πŸ˜€" | \ "πŸ˜ƒ" ]

Any suggestions would be greatly appreciated.

Thank you.

The following works with the polymorphic variants. Not sure whether there’s a way to make it work with regular variants.

type emojis = [#"πŸ˜€" | #"πŸ˜ƒ"]
let x: emojis = #"πŸ˜€"

Edit: although, not sure. #"πŸ˜€" compiles to β€œ\xf0\x9f\x98\x80”, and console.log("\xf0\x9f\x98\x80") prints Γ°

2 Likes

Normally in strings you’d use backticks instead of double quotes to fix the problem, but it looks like you cannot use backticks in polymorphic variants, although you can circumvent it by using a %raw call:

let x: emojis = %raw(`"πŸ˜€"`);
2 Likes

In the next release, strings will be UTF8 by default, even using regular double quotes, so this will be possible I think.

4 Likes