Make output definitions overridable
notquiteamonad opened this issue · 4 comments
On an incoming port, it is sometimes desirable to take a string which will be converted to a sum type in Elm. Currently, TIE's generated definition will just be string
for this case, but it would be nice if we could tell it to use something else such as "success" | "error"
I think, in fact, this could become a key feature. It would allow for decoding of sum types and maintaining type safety from a TypeScript perspective. To give a simple example, for the following Elm type representation of a message and variant for a status:
type alias StatusMessage =
{ message : String
, variant : SMVariant
}
type SMVariant = Success | Warning | Error
TIE would fail to process this if given by a port, but you could, for example, add another type to use in a port:
type alias StatusMessagePort =
{ message: String
, variant: String
}
statusMessagePortToStatusMessage : StatusMessagePort -> StatusMessage
statusMessagePortToStatusMessage {message, variant} = StatusMessage message <|
case port of
"success" -> Success
"warning" -> Warning
_ -> Error
-- of course, you could go further and add a Result to this
port statusMessage (StatusMessagePort -> msg) -> Sub msg
type Msg =
...
SetStatusMessage StatusMessage
subscriptions = [statusMessage (statusMessagePortToStatusMessage >> SetStatusMessage)]
From this, TIE would generate:
// inside Elm.Main
interface StatusMessagePort {
message: string;
variant: string;
}
But if you could add a config file for TIE and tell it instead to generate
interface StatusMessagePort {
message: string;
variant: "success" | "warning" | "error";
}
Then you maintain the safety of using an Elm decoder as long as the TS definition is correct
Toml config ->
Type definitions key ->
Key is not a table ->
Matches an additional member ->
Replace like for like ->
Does not match ->
Warn
Key is a table ->
Matches an interface ->
Subkey matches a member ->
Replace like for like
Does not match ->
Warn
Does not match an interface ->
Warn
Still to do:
- Haddock
- Test
- User documentation
Released in Version 1.1.0