-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type is not inferred correctly from a dynamically created decoder with generics #930
Comments
Hmm, looks like you may have found a bug. I would expect your example to work correctly, indeed. Does your example work correctly at runtime? This may be a bug in the signature type for I'll have to take a deeper look at this issue later. Might be after the holidays before I have the proper time for it, but I'll try to look at it sooner! Thanks for reporting this! |
Just wanted to let you know that I've started to look into this, but a fix isn't trivial, and I need a bit more time to come up with a good solution for this problem (and also one that would work whether you have The problem has to do with the fact that the { value: F } or { value?: F } This is why TypeScript (correctly) fails. Unfortunately, it leads to a cryptic error message — totally unreadable! To illustrate the issue further, here you can see the actual runtime behavior of the Therefore, you could fix this by changing the type GenericValueContainer<F> = {
value?: F
// ^ Note the question mark
} This will make the error go away. However, I realize this may not be ideal for your use case. To properly fix this, it may require the addition of a new decoder in the |
Ah yes that seems to be the issue. I missed the AllowImplicit in ObjectDecoderType while reading the typings. Fixing it might indeed be tricky if the implicit and explicit undefineds should be both allowed. In most cases these should be interchangeable, as far as I'm aware, but unfortunately in |
I hit into this issue too and I understand how difficult it is to be resolved by the maintainers. Here's a workaround for now: import * as JD from "decoders"
type GenericValueContainer<F> = {
value: F
}
function genericValueContainerDecoder<F>(
decoder: JD.Decoder<F>,
): JD.Decoder<GenericValueContainer<F>> {
return JD.define((blob, ok, err) => {
// Decode the wrapped field as an unknown first
const result = JD.object({
value: JD.unknown,
}).decode(blob)
if (result.ok === false) {
return err(result.error)
}
// Decode the wrapped value manually
const { value } = result.value
const valueF = decoder.decode(value)
return valueF.ok ? ok({ value: valueF.value }) : err(valueF.error)
})
} |
A bit cryptic description, I know, but maybe the following example clarifies the issue:
as commented, in the first case the type is inferred correctly when the generic value is wrapped in another decoder, but if the field is declared with the dynamically passed decoder directly, type inference no longer works.
This was tried with:
Let me know if there is another approach to achieving what I'm trying to do here or if any further info is required.
The text was updated successfully, but these errors were encountered: