Is it possible to type string literals using Valibot/v.pipe? #620
-
I am wondering if it's possible to do something like: import * as v from "valibot";
export const GlobalShopifyProductID = v.pipe(v.string(), v.startsWith("gid://shopify/Product/"));
type GlobalShopifyProductIDType = v.InferOutput<typeof GlobalShopifyProductID>;
// ^ type GlobalShopifyProductIDType = string But instead get something like |
Beta Was this translation helpful? Give feedback.
Answered by
fabian-hiller
Jun 3, 2024
Replies: 1 comment 1 reply
-
Yes, this is possible but only when using import * as v from 'valibot';
export const GlobalShopifyProductID = v.custom<`gid://shopify/Product/${string}`>(
(input) => typeof input === 'string' && input.startsWith('gid://shopify/Product/')
);
type GlobalShopifyProductIDType = v.InferOutput<typeof GlobalShopifyProductID>;
// ^ type GlobalShopifyProductIDType = `gid://shopify/Product/${string}` import * as v from 'valibot';
export const GlobalShopifyProductID = v.pipe(
v.string(),
v.startsWith('gid://shopify/Product/'),
v.transform((input) => input as `gid://shopify/Product/${string}`)
);
type GlobalShopifyProductIDType = v.InferOutput<typeof GlobalShopifyProductID>;
// ^ type GlobalShopifyProductIDType = `gid://shopify/Product/${string}` |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fabian-hiller
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, this is possible but only when using
custom
ortransform
: