Using with TypeScript
The Inngest SDK leverages the full power of TypeScript, providing you with some awesome benefits when handling events:
- 📑 Autocomplete
Tab ↹ your way to victory with inferred types for every event. - Instant feedback
Understand exactly where your code might error before you even save the file. - Painless type generation
Add a simple"predev"
script and never miss a change.
All of this comes together to provide some awesome type inference based on your actual production data.
Generating types
Using the Inngest CLI, you can generate TS types for your entire Inngest Cloud workspace with a single command:
shnpx inngest-cli types ts [--output my/file.ts]
This will - by default - guide you through logging in to your Inngest Cloud account and will then create a file at ./__generated__/inngest.ts
with all of your event types.
We recommend placing this in a "predev"
script in your package.json
so you're always pulling the latest types whenever your writing code.
json{"scripts": {"dev": "next dev","predev": "npx inngest-cli types ts"}}
Using types
Once your types are generated, there are a few ways we can use them to ensure our functions are protected.
new Inngest()
client
We can use these when creating a new Inngest client via new Inngest()
.
This comes with powerful inference; we autocomplete your event names when selecting what to react to, without you having to dig for the name and data.
tsimport { Inngest } from "inngest";import { Events } from "./__generated__/inngest";const inngest = new Inngest<Events>({ name: "My App" });const fn = inngest.createFunction("Send welcome email", ...);
createFunction
helper
We can also use individual events if using the createFunction
helper directly. Each event is exported individually, so you can import the particular event instead of the Events
interface.
tsimport { createFunction } from "inngest";import { DemoEvent } from "./__generated__/inngest";const fn = createFunction<DemoEvent>("Demo function", "demo/demo.event", job);
Custom types
Sometimes it's necessary to define custom types to provide type safety before an event has been seen by Inngest.
For this, we can pass extra data to the generated Events
type.
tsconst inngest = new Inngest<Events<{"app/user.created": { data: { id: string } };"app/user.updated": AppUserUpdated;}>>({ name: "My App" });
TypeScript will also enforce your custom events being the right shape - see Event Format for more details.
By design, these custom types are silently overidden as the events pass through Inngest and become generated types. This helps to ensure you're accounting for the actual data found in the payloads as opposed to what you hope is present when you first define the type.