I want to re-export zod
namespace, but add a few properties to it.
I tried this:
import * as zod from 'zod';
export namespace z {
export * from 'zod';
export const numericId = () => zod.coerce.number().int().safe();
}
However, this approach is giving TypeScript error:
Export declarations are not permitted in a namespace.ts(1194)
It also fails to bundle using ESBuild with an error:
Unexpected “export”
I was able to make it partially work using this approach:
export namespace z {
// @ts-expect-error - TODO TypeScript complains that export is not allowed. However, it works.
export type * from 'zod';
export const string = zod.string;
export const numericId = () => zod.coerce.number().int().safe();
}
But ran into limitation that export const enum
and export const null
is not allowed because the variable names are reserved.
export const { array, boolean, enum, null, number, object, string, record, literal, union } = zod;
(^This does work because null
and enum
cannot be exported as variables)