Skip to content
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

fix(TransformObjectDeep): remove | undefined in Array case #91

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DrJume
Copy link

@DrJume DrJume commented Apr 19, 2023

Thank you so much for this library!
I am using it to patch deeply nested auto-generated types from https://github.com/payloadcms/payload.

Based on the name of a property, I am applying Required<T> for this property.

E.g: Transform

{
  x: {
    id?: string | undefined,
    ...
  },
  ...
}

to

{
  x: {
    id: string,
    ...
  },
  ...
}

I only found the internal type TransformObjectDeep to help me with this use case.

import { type ComposeLeft, type Fn } from 'hotscript'
import { type TransformObjectDeep } from 'hotscript/dist/internals/objects/impl/objects'
import { type SetRequired } from 'type-fest'

interface IdRequiredFn extends Fn {
  return: this['args'] extends [infer obj]
    ? obj extends { id?: string }
      ? SetRequired<obj, 'id'>
      : obj
    : never
}

type FixPayloadTypes<T> = TransformObjectDeep<
  ComposeLeft<[IdRequiredFn]>, // I am also using other transform Fns
  T
>

But by using TransformObjectDeep all properties with Array<T> type get "unioned" with undefined, causing access to the items to fail.
What is the reason for adding undefined to the type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant