We've already briefly looked at polymorphic variants in the previous chapter. To recap, we learned about them when we used the [@bs.unwrap] decorator to bind to some existing JavaScript. The idea was that [@bs.unwrap] can be used to bind to an existing JavaScript function where its arguments can be of different types. For example, let's say we want to bind to the following function:
function dynamic(a) {
switch (typeof a) {
case "string":
return "String: " + a;
case "number":
return "Number: " + a;
}
}
Let's say this function should only accept arguments of the string type or int type and nothing else. We could bind to this example function as follows:
[@bs.val] external dynamic : 'a => string = "";
However, our binding would then allow invalid argument types (such as...