inngest / inngest-js

The developer platform for easily building reliable workflows with zero infrastructure for TypeScript & JavaScript
https://www.inngest.com/
GNU General Public License v3.0
414 stars 41 forks source link

Ensure `fromRecord<>()` doesn't accept clashing names #337

Closed jpwilliams closed 12 months ago

jpwilliams commented 12 months ago

Summary

new EventSchemas().fromRecord() can silently fail and drop all type inference if the user has given a name inside an event as well as a key and the two are not the same.

In this case, it's hard to know what the user is intending to do and which name they'd prefer, so instead we show an error stating that they should probably omit name.

Previously, this would not fail, but drop all type inference.

type MyEvent = {
  name: "my-event";
};

new EventSchemas().fromRecord<{
  "my.event": MyEvent;
}>();

Instead, this will throw a hacky TS error stating you should probably omit name.

Expected 1 arguments, but got 0.

(method) EventSchemas<Record<string, EventPayload>>.fromRecord<{
    "my.event": MyEvent;
}>(_args_0: "Error: Omit 'name' from event schemas or make sure it matches the key."): EventSchemas<Simplify<{
    "my.event": {};
}>>

A tad hacky, but better than type inference silently turning off.

Checklist

changeset-bot[bot] commented 12 months ago

🦋 Changeset detected

Latest commit: 4b10014d029054503dbe83e2e5486b86259dd74b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ------- | ----- | | inngest | Patch |

Not sure what this means? Click here to learn what changesets are.

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/inngest/inngest-js/new/fix-fromrecord-name-clash?filename=.changeset/lemon-plums-care.md&value=---%0A%22inngest%22%3A%20patch%0A---%0A%0AEnsure%20%60fromRecord%3C%3E()%60%20doesn't%20accept%20clashing%20names%0A)