Michael McAndrewM
tRPC3y ago
2 replies
Michael McAndrew

Is `inferSubscriptionOutput` missing in 10.x?

I'm working on a vue 3 app with reactive state and need to infer the output of a subscription.

It looks like 9.x had inferSubscriptionOutput but that appears to be deprecated / not applicable in 10.x and I am not sure what the best alternative is.

There is a inferObservableValue but I am not sure how that works / if it is what I am looking for.

Here is some todo app code to help explain the problem:

import { ref } from 'vue'
import { trpc } from './trpc' // the TRPC proxy client

const todos = ref<Type>()

trpc.todos.subscribe(undefined, {
  onData(data) {
    todos.value = data;
  },
});


I need to infer the type (Type) of onData's
data
before the subscription is called so I can type the reactive ref that will receive the todos.

I have created the following inferSubscriptionObservable type that extracts the observable from the client:

type inferSubscriptionOutput<T extends (...args: any) => any> = Parameters<
  NonNullable<Parameters<T>[1]["onData"]>
>[0];

const todos = ref<inferSubscriptionOutput<typeof trpc.todos.subscribe>>()

It's working for me but I suspect there is a better way of doing this using the
AppRouter
type. Something similar to inferSubscriptionOutput from 9.x? so I could do

const todos = ref<inferSubscriptionOutput<AppRouter["todos"]>>();


Do you agree that this is missing from 10.x?

I am not sure that I have enough familiarity with typescript and trpc to work this out myself but happy to file a feature request and help out, etc.
Was this page helpful?