<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/adding-a-second-middleware-broke-our-typescript-types-sniytmfbv" -->

---
title: Adding a second middleware broke our typescript types
description: A deep-dive debugging story explains why stacking two middleware on an Inngest client silently collapses TypeScript's inferred step.run return type to {}. The...
canonical: https://daily.dev/posts/adding-a-second-middleware-broke-our-typescript-types-sniytmfbv
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Adding a second middleware broke our typescript types | daily.dev
og:description: A deep-dive debugging story explains why stacking two middleware on an Inngest client silently collapses TypeScript's inferred step.run return type to {}. The...
og:url: https://daily.dev/posts/adding-a-second-middleware-broke-our-typescript-types-sniytmfbv
og:image: https://api.daily.dev/og/posts/sNIYTMFbV.png
og:image:alt: Adding a second middleware broke our typescript types
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Adding a second middleware broke our typescript types

**[Inngest Blog](https://daily.dev/sources/inngest)** · 11 min read · 0 upvotes · 0 comments

## Summary

A deep-dive debugging story explains why stacking two middleware on an Inngest client silently collapses TypeScript's inferred step.run return type to {}. The root cause traces to a common 'filter keys by value type' idiom that leaks undefined into a key union when a property is optional; because generic constraints are only checked at definition time, not at instantiation, this poisoned key set gets picked up silently and collapses when Jsonify composes with itself twice. Several community-proposed fixes (deduplicating transforms, guarding with JsonValue checks) each missed edge cases involving unknown types or step.invoke. The actual fix excludes undefined from the filtered key union, and an even cleaner follow-up rewrites the filter using key remapping with 'as' to avoid ever creating a value union with undefined in it. The piece also shows how naive IsEqual-based type tests can pass on genuinely broken types unless they force resolution via property access.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.inngest.com/blog/adding-a-second-middleware-broke-our-typescript-types>

## Questions this post answers

### Why does adding a second middleware to an Inngest client cause step.run's return type to collapse to an empty object?

Each middleware applies a Jsonify type transform, and by default these compose, producing Jsonify<Jsonify<T>>. If T has an optional property, the standard key-filtering idiom ({ ... }[keyof T]) leaks undefined into the resulting key union. Pick then silently tolerates that undefined key because generic constraints are only checked where the generic is defined, not when it is later instantiated with concrete types, and on the second Jsonify application this produces TypeScript's internal error type, which collapses into an empty {} object type.

_daily.dev surfaces deep TypeScript debugging writeups like this for developers chasing silent generic type bugs._

### Why does the { ... }[keyof T] idiom for filtering TypeScript object keys by value type introduce undefined into the result?

Mapped types written as [Key in keyof T] preserve each property's optional modifier, so an optional key like label maps to label? in the intermediate type. Reading every value out of that object with [keyof T] then includes undefined in the union, because accessing an optional property always includes undefined. Feeding that union to Pick silently accepts the undefined since Pick's constraint is only checked at the generic's definition site, not re-checked when concrete types are substituted later.

_developers writing generic TypeScript key filters can check similar type-safety pitfalls curated on daily.dev._

### How do you fix a TypeScript type filter idiom so that optional properties don't leak undefined into a key union?

Wrap the filtered key union in Exclude<..., undefined> before passing it to Pick, or better, avoid the value-union step entirely by remapping keys in place with the 'as' clause: { [Key in keyof T as T[Key] extends NotJsonable ? never : Key]: Jsonify<T[Key]> }. The 'as' approach tests each key while it is still a key rather than after being read out as a value, so undefined never has a chance to enter the key set at all.

_daily.dev helps developers track fixes like this Exclude and key-remapping pattern for type-safe generics._

## Similar posts on daily.dev

- [Stop Type Drift with Linked Typing](https://daily.dev/posts/stop-type-drift-with-linked-typing-lzs6ewbob) · TypeScript.TV · 1 upvotes · 0 comments
- [Typed Polymorphic React Component](https://daily.dev/posts/typed-polymorphic-react-component-qwqrzo59w) · Kitty says hi. · 68 upvotes · 1 comments
- [Seeing types where others don't](https://daily.dev/posts/seeing-types-where-others-don-t-7srbyqoa7) · Lobsters · 0 upvotes · 0 comments
- [Type systems are leaky abstractions: the case of Map.take\!/2](https://daily.dev/posts/type-systems-are-leaky-abstractions-the-case-of-map-take-2-5wbmgto0e) · Dashbit · 0 upvotes · 0 comments
- [NestJS Dependency Injection: Source Code, Type Erasure Hacks, and Building Your Own IoC Container from Scratch](https://daily.dev/posts/nestjs-dependency-injection-source-code-type-erasure-hacks-and-building-your-own-ioc-container-fr-74fnlhzd6) · Medium · 0 upvotes · 0 comments

---

Tags: [#inngest](https://daily.dev/tags/inngest), [#typescript](https://daily.dev/tags/typescript)

[View this post on daily.dev](https://daily.dev/posts/adding-a-second-middleware-broke-our-typescript-types-sniytmfbv)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Adding a second middleware broke our typescript types","url":"https://daily.dev/posts/adding-a-second-middleware-broke-our-typescript-types-sniytmfbv","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/adding-a-second-middleware-broke-our-typescript-types-sniytmfbv"},"datePublished":"2026-09-13T19:57:32.148Z","dateModified":"2026-10-15T02:23:44.184Z","description":"A deep-dive debugging story explains why stacking two middleware on an Inngest client silently collapses TypeScript's inferred step.run return type to {}. The...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0692956644518e95097779718cd7d501?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0692956644518e95097779718cd7d501?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Inngest Blog","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Inngest Blog","logo":"https://media.daily.dev/image/upload/s--ieZKUa5y--/c_limit,w_256/f_auto,q_auto/v1789329423/logos/inngest?_a=BAMAMicg0","url":"https://daily.dev/sources/inngest"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/adding-a-second-middleware-broke-our-typescript-types-sniytmfbv","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"inngest,typescript","timeRequired":"PT11M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Inngest Blog","item":"https://daily.dev/sources/inngest"},{"@type":"ListItem","position":3,"name":"Adding a second middleware broke our typescript types"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/adding-a-second-middleware-broke-our-typescript-types-sniytmfbv#faq","mainEntity":[{"@type":"Question","name":"Why does adding a second middleware to an Inngest client cause step.run's return type to collapse to an empty object?","acceptedAnswer":{"@type":"Answer","text":"Each middleware applies a Jsonify type transform, and by default these compose, producing Jsonify\u003cJsonify\u003cT\u003e\u003e. If T has an optional property, the standard key-filtering idiom ({ ... }[keyof T]) leaks undefined into the resulting key union. Pick then silently tolerates that undefined key because generic constraints are only checked where the generic is defined, not when it is later instantiated with concrete types, and on the second Jsonify application this produces TypeScript's internal error type, which collapses into an empty {} object type. daily.dev surfaces deep TypeScript debugging writeups like this for developers chasing silent generic type bugs."}},{"@type":"Question","name":"Why does the { ... }[keyof T] idiom for filtering TypeScript object keys by value type introduce undefined into the result?","acceptedAnswer":{"@type":"Answer","text":"Mapped types written as [Key in keyof T] preserve each property's optional modifier, so an optional key like label maps to label? in the intermediate type. Reading every value out of that object with [keyof T] then includes undefined in the union, because accessing an optional property always includes undefined. Feeding that union to Pick silently accepts the undefined since Pick's constraint is only checked at the generic's definition site, not re-checked when concrete types are substituted later. developers writing generic TypeScript key filters can check similar type-safety pitfalls curated on daily.dev."}},{"@type":"Question","name":"How do you fix a TypeScript type filter idiom so that optional properties don't leak undefined into a key union?","acceptedAnswer":{"@type":"Answer","text":"Wrap the filtered key union in Exclude\u003c..., undefined\u003e before passing it to Pick, or better, avoid the value-union step entirely by remapping keys in place with the 'as' clause: { [Key in keyof T as T[Key] extends NotJsonable ? never : Key]: Jsonify\u003cT[Key]\u003e }. The 'as' approach tests each key while it is still a key rather than after being read out as a value, so undefined never has a chance to enter the key set at all. daily.dev helps developers track fixes like this Exclude and key-remapping pattern for type-safe generics."}}]}
```

