Nivoa
Nivoa2mo ago

When a procedure returns an instance of a class, the superjson isApplicable doesnt detect it

When a procedure returns an instance of a class, the superjson isApplicable doesnt detect it as that class, but when you return it inside an object or array, it does.
// ❌
class Test {
a: string;
constructor() {
this.a = "a";
}
serialize() {
return "a"
}
deserialize() {
return "b"
}
}
test: publicProcedure
.query(async ({ input, ctx }) => {
return new Test()
}),
// ❌
class Test {
a: string;
constructor() {
this.a = "a";
}
serialize() {
return "a"
}
deserialize() {
return "b"
}
}
test: publicProcedure
.query(async ({ input, ctx }) => {
return new Test()
}),
// ✅
test: publicProcedure
.query(async ({ input, ctx }) => {
return { test: new Test() }
}),
// ✅
test: publicProcedure
.query(async ({ input, ctx }) => {
return { test: new Test() }
}),
then in superjson:
superjson.registerCustom(
{
deserialize: (test) => {
return null;
},
serialize: (test) => {
return null;
},
isApplicable: (val: unknown) => {
return val instanceof Test
},
},
"Test",
);
superjson.registerCustom(
{
deserialize: (test) => {
return null;
},
serialize: (test) => {
return null;
},
isApplicable: (val: unknown) => {
return val instanceof Test
},
},
"Test",
);
what happens is that val is never an actual instanceof Test, it is an object with the properties of Test, but not the instance itself. But when I return it as the 2nd procedure, val.test instanceof Test evals to true. is this a bug?
4 Replies
Alex / KATT 🐱
If it's a bug it's likely a bug in superjson Or maybe this is in trpc, idk I've never serialized classes
Alex / KATT 🐱
You should be able to create a failing test pretty easily in https://github.com/trpc/trpc/blob/next/packages/tests/server/transformer.test.ts
GitHub
trpc/packages/tests/server/transformer.test.ts at next · trpc/trpc
🧙‍♀️ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. - trpc/trpc
Alex / KATT 🐱
Run local vitest with pnpm test-watch transformer
Nivoa
Nivoa2mo ago
ty, ill try It seems a bug in unstable_httpBatchStreamLink because with httpBatchLink it works as expected i'll investigate more