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(types): make assign return type more accurate + add Assign type #142

Merged
merged 2 commits into from
Aug 15, 2024

Conversation

aleclarson
Copy link
Member

@aleclarson aleclarson commented Jul 29, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

  • Add the Assign type, which tries to be as accurate as possible.
  • Add the BuiltInType utility type as a union type of all built-in types (including DOM-specific and NodeJS-specific types when possible).
  • Add the OptionalKeys and RequiredKeys utility types.

CustomClassRegistry

Since there's no built-in way to differentiate a “plain object” type and a custom class instance type, I've added the CustomClassRegistry interface. Anyone can extend this in their project by using “declaration merging”:

class FooBar {
  foo: unknown
  bar: unknown
}

declare module 'radashi' {
  interface CustomClassRegistry {
    FooBar: FooBar
  }
}

Now the assign function's return type won't suggest that FooBar instances were merged with. Check out this before-and-after example:

// BEFORE
const result = assign(
  { a: new FooBar },
  { a: { foo: '' } }
)
// ^? result: { a: { foo: string; bar: unknown } }

// AFTER
const result = assign(
  { a: new FooBar },
  { a: { foo: '' } }
)
// ^? result: { a: { foo: string } }

BEWARE: Any class you add to CustomClassRegistry must not have identical properties as any other “plain object” types you ever plan to use, because as far as TypeScript is concerned, they are identical. This could lead to suboptimal result types.

Related issue, if any:

Resolves #141 (cc @crishoj)

For any code change,

  • Related tests have been added or updated, if needed

Does this PR introduce a breaking change?

Yes

Bundle impact

Status File Size Difference (%)
M src/object/assign.ts 373 1 +0 (+0%)

Footnotes

  1. Function size includes the import dependencies of the function.

@aleclarson aleclarson mentioned this pull request Jul 29, 2024
3 tasks
@aleclarson aleclarson added future planning There's no intention to resolve this in the near-term. and removed future planning There's no intention to resolve this in the near-term. labels Aug 6, 2024
@aleclarson aleclarson force-pushed the fix/assign-return-type branch 2 times, most recently from 554c7ba to b8b225c Compare August 15, 2024 20:34
@aleclarson aleclarson merged commit f1e4957 into main Aug 15, 2024
7 checks passed
@aleclarson aleclarson deleted the fix/assign-return-type branch August 15, 2024 20:51
Copy link

A new beta version 12.2.0-beta.6b76988 has been published to NPM. 🚀

To install:

pnpm add radashi@12.2.0-beta.6b76988

The radashi@beta tag also includes this PR.

See the changes

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.

2 participants