T
tRPC

❓-help

Incorrect type inference with discriminated unions.

Ddecho12/24/2023
Hey folks, I am having a really weird issue. Here is a minimal reproducible example: https://tsplay.dev/WJB1kW Basically I have a function that returns an object of the Difference, which is a union of 3 other types:
interface DifferenceCreate {
type: "CREATE";
value: any;
}

interface DifferenceRemove {
type: "REMOVE";
oldValue: any;
}

interface DifferenceChange {
type: "CHANGE";
value: any;
oldValue: any;
}

export type Difference = DifferenceCreate | DifferenceRemove | DifferenceChange;

function myFunction(): Difference {
return { type: 'CREATE', value: 1 }
}
interface DifferenceCreate {
type: "CREATE";
value: any;
}

interface DifferenceRemove {
type: "REMOVE";
oldValue: any;
}

interface DifferenceChange {
type: "CHANGE";
value: any;
oldValue: any;
}

export type Difference = DifferenceCreate | DifferenceRemove | DifferenceChange;

function myFunction(): Difference {
return { type: 'CREATE', value: 1 }
}
I have created a router and a procedure that returns the result of myFunction. I then call this function on the client like this:
const test:Difference = await client.test.query();
const test:Difference = await client.test.query();
However, I am getting a type error:
Type '{ type: "CREATE"; value?: any; } | { type: "REMOVE"; oldValue?: any; } | { type: "CHANGE"; value?: any; oldValue?: any; }' is not assignable to type 'Difference'.
Type '{ type: "CREATE"; value?: any; }' is not assignable to type 'Difference'.
Type '{ type: "CREATE"; value?: any; }' is not assignable to type 'DifferenceCreate'.
Property 'value' is optional in type '{ type: "CREATE"; value?: any; }' but required in type 'DifferenceCreate'.
Type '{ type: "CREATE"; value?: any; } | { type: "REMOVE"; oldValue?: any; } | { type: "CHANGE"; value?: any; oldValue?: any; }' is not assignable to type 'Difference'.
Type '{ type: "CREATE"; value?: any; }' is not assignable to type 'Difference'.
Type '{ type: "CREATE"; value?: any; }' is not assignable to type 'DifferenceCreate'.
Property 'value' is optional in type '{ type: "CREATE"; value?: any; }' but required in type 'DifferenceCreate'.
Any idea how to solve this issue?
tsplay.dev
A link shortener for the TypeScript Playground
Nnlucas12/24/2023
Hey could you please put this in a GitHub issue? Fantastic reproduction πŸ™‚
Ddecho12/24/2023
for sure I can, I just thought I might be doing something wrong so didn't want to create a useless issue
Nnlucas12/24/2023
All looks sane to me, we might be able to improve this
Ddecho12/24/2023
all right, i will do it in a few min, do you think the title is good enough? as in, is it descriptive enough, right terminology and all?
Solution
Ddecho12/24/2023
GitHub
bug: Incorrect type inference with discriminated unions. Β· Issue #5...
Provide environment information System: OS: Linux 6.5 Linux Mint 21.2 (Victoria) CPU: (24) x64 AMD Ryzen 9 7900 12-Core Processor Memory: 24.78 GB / 30.53 GB Container: Yes Shell: 5.8.1 - /bin/zsh ...
AKAlex / KATT 🐱12/25/2023
ah, this is something to do with the SerializeJson we use
AKAlex / KATT 🐱12/25/2023
you can see this works https://tsplay.dev/mxzyZw
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
AKAlex / KATT 🐱12/25/2023
oops, i missed that there was replies πŸ™‚
Ddecho12/25/2023
hey man I left a reply in the github issue, but just wanted to say thanks to both of you for helping out, i appreciate it πŸ‘

Looking for more? Join the community!

T
tRPC

❓-help

Join Server
Recommended Posts
Get Query Key on ServerI'm following some recommended practices from one of the react query maintainers `@tkdodo` and want ReferenceError: Cannot access 'appRouter' before initializationHi friends im having trouble implementing a "protected" or "private" procedure in TRPC using Next.jstrpc openapi does not work on app routerhttps://github.com/jlalmes/trpc-openapi Can i just know if trpc open api for next is also applicableSubscription types are not inferred while queries and mutations are working fineI am NOT working with a monorepo but while query and mutation types are being inferred out of the boHow are thrown (unhandled) exceptions safe to pass?Hi, I started looking into error handling more and understand that all errors get wrapped in a TRPCECanary useUtils and createTRPCQueryUtils type not callableTrying to access utils object in my application after using `useUtils` or `createTRPCQueryUtils` andtRPC run express under the hood?tRPC run `expressjs` under the hood?Modify mutation error messages?We occasionally get timeouts from cloudfront which gives the user an error about how the cloudfront server-side helpers doesnt work with `Hydrate` and `QueryClient`environtment: node v20.9.0, npm Hi i tried to implement server-side helpers but somehow it just doesUnable to mock unstable_batchStreamLink network responseHi there! I have a couple API endpoints that call third party dependencies and would like to mock a Using trpc with fastify server and nextjs clientHello, is it possible to use fastify for server and have a nextjs client and make use of the SSG / SI can't mutate or query in SvelteKitEnvironment: Node 20 with Yarn I'm not getting any specific errors but I don't get the "mutate" or "TRPCError class, public readonly cause?: Error; this line should have override functionhey i am trying to use the TRPCError for throwing errors in my apps, but i am getting this error, chWhy do people use SplitLink? (http + websocket)Why not just use websockets for everything? Just curious.Force user to loginHow can I redirect the user to "/login" if the user are not logged in?TRPC with middly middleware on lambdaHello, has anyone used middly middleware on AWS lambda? Thanks!tRPC on Vercel Serverless without Next?I would love to be able to use tRPC in a vercel serverless context, but not in a Next app – just theUsage with zod-validation-errorHas anyone tried using trpc with https://www.npmjs.com/package/zod-validation-error ?Errors while running mutations when deploying CT3A using SSTI'm currently migrating a NextJS project of mine from Vercel to SST. It's based of CT3A and the trpcKeep suspense on SSRI'm currently trying to perform a trpc query in Nextjs with React suspense, however, I only want the