Skip to content

Type is not inferred correctly from a dynamically created decoder with generics #930

@vjrasane

Description

@vjrasane

A bit cryptic description, I know, but maybe the following example clarifies the issue:

import { array, Decoder, object } from "decoders"

type GenericArrayContainer<F> = {
    value: F[]
}

// inferred type matches declared type
const genericArrayContainer = <F>(decoder: Decoder<F>): Decoder<GenericArrayContainer<F>> => object({
    value: array(decoder),
})

type GenericValueContainer<F> = {
    value: F
}

// inferred type does not match declared type
const genericValueContainer = <F>(decoder: Decoder<F>): Decoder<GenericValueContainer<F>> => object({
    value: decoder,
})

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:

    "decoders": "^2.0.1",
    "typescript": "^4.9.4"

Let me know if there is another approach to achieving what I'm trying to do here or if any further info is required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions