Reply sum types need a discriminator
Closed this issue · 2 comments
I wanted to implement the loadFile
command for atom.
I'm using
import { IdrisClient } from "idris-ide-client"
const client = new IdrisClient()
client.loadFile(filePath).then((loadFile: FinalReply.LoadFile) => {
// here
})
but I can't really use loadFile
where // here
is because typescript needs a discriminator
to be able to properly use sum types.
So
export type LoadFile =
| {
ok: null
id: number
type: ":return"
}
| { err: string; id: number; type: ":return" }
this would need to be something like this:
export type LoadFile =
| {
ok: true
id: number
type: ":return"
}
| { ok: false, err: string; id: number; type: ":return" }
or:
export type LoadFile =
| {
result: 'ok'
id: number
type: ":return"
}
| { result: 'error', err: string; id: number; type: ":return" }
Yeah, I'm not really satisfied with those types either. In the meantime, you can discriminate based on the presence of the key rather than the value:
if ("ok" in reply) {
// do stuff
}
I do find that clunky though.
I like the idea of having an ok: boolean
attribute. It would make it more pleasant to test the reply for success: if (reply.ok)
is clearer. It's similar to how the fetch
api works. And, I didn't like putting the reply payload in ok
.
I didn't know about this :)
if ("ok" in reply) {
// do stuff
}
thanks.
I like the idea of having an ok: boolean attribute. It would make it more pleasant to test the reply for success: if (reply.ok) is clearer. It's similar to how the fetch api works. And, I didn't like putting the reply payload in ok.
and I agree with this