Mapped Types Not Inferring From Promise Correctly

TS mapped types not behaving as expected on promise inference

Desired DX

export const FileRouter = UploadThingServerHelper({
  someKey: {
    middleware: async (req) => {
      const user = await auth(req);

      if (!user.userId) throw new Error("someProperty is required");

      return { userIds: user.userId };
    },
    onUpload: async (request) => {
      console.log("uploaded with the following metadata:", request.metadata); // THIS SHOULD INFER THE TYPE FROM THE RETURN
    },
  },
});


Package types

type UploadableInputParams<TMiddlewareResponse> = {
  middleware: (request: Request) => Promise<TMiddlewareResponse>;
  onUpload: (response: {
    metadata: TMiddlewareResponse;
  }) => void;
};

export const UploadThingServerHelper = <TValidRoutes>(
  input: {
    readonly [K in keyof TValidRoutes]: UploadableInputParams<TValidRoutes[K]>;
  },
  config?: { callbackUrl?: string }
) => {...businessLogic};


Currently have "unknown" coming through in the onUpload type. If I manually define the type on request (i.e. onUpload: async (request: {metadata: {userId: string}})), it will "backfill" correctly

Failing to get the type to infer off Promise<T> to the onUpload input
Was this page helpful?