Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Constructors holding Partial<T> can be called with any argument  #48

@geigerzaehler

Description

@geigerzaehler

When using Partial as a value type the calling the constructors with any object type checks but it should not. For example the following should not type check

type T = { field: string }

const U = unionize({
  PartialT: ofType<Partial<T>>(),
})

// This should fail
const u = U.PartialT({ foo: true })
const u2 = U.PartialT(true)

The underlying problems seems to be that the constraint {} extends UnTagged<Partial<T>, 'tag'> does not hold. If we look at the definition of Creators we see that this results in the constructor accepting an argument of type undefined | {} which seems wrong.

export type Creators<Record, TaggedRecord, TagProp extends string> = {
  [T in keyof Record]: {} extends UnTagged<Record[T], TagProp>
    ? ((value?: {}) => TaggedRecord[keyof TaggedRecord])
    : ((value: UnTagged<Record[T], TagProp>) => TaggedRecord[keyof TaggedRecord])
}

I’m not sure what the solution would be but would be happy to create a PR if somebody has a suggestion.

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