A
arktype5mo ago
Tim

Pipe generic types

I'm trying to extract a generic type, but it seems like it's not possible, or am I mistaken? What I'm trying to do is to extract a generic property
const genericType = type("<t>", {jsonValue:"t"}).pipe(({jsonValue}) => jsonValue)
const genericType = type("<t>", {jsonValue:"t"}).pipe(({jsonValue}) => jsonValue)
But pipe does not exist on Generic
6 Replies
ssalbdivad
ssalbdivad5mo ago
This would be the way to define it syntactically, though if you want the inference to work here for specific types you will likely need to use an HKT or an external generic wrapper:
const genericType = type("<t>", [
{ jsonValue: "t" },
"=>",
({ jsonValue }) => jsonValue,
]);

const z = genericType({ jsonValue: "true" });
const genericType = type("<t>", [
{ jsonValue: "t" },
"=>",
({ jsonValue }) => jsonValue,
]);

const z = genericType({ jsonValue: "true" });
Tim
TimOP5mo ago
Awesome, thanks! I suppose I could also add another => t after the jsonValue extraction? However that would introduce redundant validation logic What is this syntax called / where can I find it in the docs? Ctrl + K is not getting any hits
ssalbdivad
ssalbdivad5mo ago
Should be doable one of those other ways if you need the narrowed inference for any expression it is called the tuple syntax, you can see it by clicking that tab It's primarily useful because it allows you to write arbitrary expressions like this with pure syntax in contexts like a scope or generic where you can't directly introduce a type wrapper At some point I will do a more general intro on different syntax types and when to use them since I know that can be a bit confusing
Tim
TimOP5mo ago
Hm I can't find a tuple tab, only tuple types, not tuple syntax
ssalbdivad
ssalbdivad5mo ago
No I mean it's embedded in each expression as an option:
No description
Tim
TimOP5mo ago
Ah I see, thanks

Did you find this page helpful?