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

rewrite OptionalProperties type #241

Open
github-actions bot opened this issue Apr 14, 2023 · 0 comments
Open

rewrite OptionalProperties type #241

github-actions bot opened this issue Apr 14, 2023 · 0 comments

Comments

@github-actions
Copy link

should probably be moved to types and then exported when its fixed

https://api.github.com/DetachHead/ts-helpers/blob/6c4b860f5fda1f2e96a416e677cd60af2e216b97/src/functions/misc.ts#L333

    class_: T,
    ...args: ConstructorParameters<T>
): HasTypedConstructor<T> => new class_(...args) as HasTypedConstructor<T>

declare const replacedUndefined: unique symbol

/**
 * recursively removes `undefined` properties from an object type. used by the `optionalProperties` function
 * only useful when using the `exactOptionalPropertyTypes` compiler option. this type has no effect if it's disabled
 *
 * @example
 * type Foo = RemoveUndefinedPropertiesRecursive<{ a?: number | undefined, b: string | undefined }> // { a?: number, b: string | undefined }
 */
// TODO: rewrite this, it sucks (currently turns properties into `never` instead of removing them)
// should probably be moved to types and then exported when its fixed
type OptionalProperties<T extends object> =
    | TsToolbeltFilter<
          {
              [K in keyof T]: ListOf<T[K]> extends infer Union
                  ? // now iterate over the union and recursively call this type on any object types within the union:
                    {
                        [UnionIndex in keyof Union]: Union[UnionIndex] extends object
                            ? OptionalProperties<Union[UnionIndex]>
                            : Replace<Union[UnionIndex], undefined, typeof replacedUndefined>
                    }[Keys<Union>]
                  : never
          },
          typeof replacedUndefined,
          '<-contains'
      > &
          ReplaceValuesRecursive<T, undefined, never>

/**
 * recursively removes `undefined` properties from an object.
 * useful when using the `exactOptionalPropertyTypes` compiler option
 */
export const optionalProperties = <T extends object>(object: T): OptionalProperties<T> =>
    (Object.entries(object) as [string, unknown][]).reduce((prev, [key, value]) => {
        let result
        if (Array.isArray(value)) {
            result = value.map(optionalProperties)
        } else if (typeof value === 'object' && value !== null) {
            result = optionalProperties(value)
        } else {
            result = value
        }
        return {
            ...prev,
            ...(value === undefined ? {} : { [key]: result }),
        }
    }, {} as OptionalProperties<T>)
@github-actions github-actions bot added the todo label Apr 14, 2023
@DetachHead DetachHead changed the title rewrite this, it sucks (currently turns properties into never instead of remov... rewrite OptionalProperties Apr 14, 2023
@DetachHead DetachHead changed the title rewrite OptionalProperties rewrite OptionalProperties type Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant