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

Typing on assign allowing for partials #424

Open
jessielaf opened this issue Aug 28, 2024 · 1 comment
Open

Typing on assign allowing for partials #424

jessielaf opened this issue Aug 28, 2024 · 1 comment

Comments

@jessielaf
Copy link

Hi,

I want to use assign to overwrite some values. However, because the overwrite in the assign function is the same as the first parameter, the second parameter cannot be partial. For example:

type Person = {
  firstName: string;
  lastName: string;
};

const person: Person = {
  firstName: "John",
  lastName: "Doe",
};

const person2 = rAssign(person, { firstName: "Jane" });

This gives the following error:

Argument of type '{ lastName: string; }' is not assignable to parameter of type 'Person'.

I now fixed this locally using the following:

import { assign as rAssign } from "radash";

// Gotten from https://stackoverflow.com/questions/41980195/recursive-partialt-in-typescript
type RecursivePartial<T> = {
  [P in keyof T]?: T[P] extends (infer U)[]
    ? RecursivePartial<U>[]
    : T[P] extends object | undefined
      ? RecursivePartial<T[P]>
      : T[P];
};

type newAssign = <X extends Record<string | number | symbol, any>>(
  initial: X,
  override: RecursivePartial<X>,
) => X;
export const assign = rAssign as newAssign;

However, it would be nice if this was baked into the library. I don't know if this should be another function called partialAssign, but I think the current assign function does have its limitations.

@MarlonPassos-git
Copy link
Contributor

@jessielaf I would recommend you to take a look at Radashi(radashi-org/radashi#168) where we fixed this problem.

image

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

No branches or pull requests

2 participants