Type of field based on sibling

Hi all, I'm trying to construct an interface for a constructor. It would work like this:
const foo = new Bar({
input: z.string(),
output: (i) => {}
})
const foo = new Bar({
input: z.string(),
output: (i) => {}
})
where i = string. In other words, I need to construct a generic signature like this.
class Bar<T extends
input: z.ZodTypeAny,
output: (i: zod.infer<T["input"]>) => any
> {
//...
}
class Bar<T extends
input: z.ZodTypeAny,
output: (i: zod.infer<T["input"]>) => any
> {
//...
}
but it doesn't work. Wondering if it's even possible...
4 Replies
Shoodey
Shoodey2y ago
https://stackoverflow.com/questions/73413992/how-to-use-parameter-as-type-definition this seems to fit - although that instantiation looks sus
Stack Overflow
How to use parameter as type definition?
Is it possible to dynamically define a parameter type based on another parameter? Like in the following scenario: import z from 'zod' // I have a function that defines a command factory with a build
benten
bentenOP2y ago
That example is for two parameters, not fields on the same object
Brendonovich
Brendonovich2y ago
How about this?
import { z } from "zod";

class Bar<Input extends z.ZodType, Output> {
constructor(args: { input: Input; output: (i: z.infer<Input>) => Output }) {}
}
const foo = new Bar({
input: z.string(),
output: (i) => {},
});
import { z } from "zod";

class Bar<Input extends z.ZodType, Output> {
constructor(args: { input: Input; output: (i: z.infer<Input>) => Output }) {}
}
const foo = new Bar({
input: z.string(),
output: (i) => {},
});
benten
bentenOP2y ago
Yep this worked great, thanks
Want results from more Discord servers?
Add your server