envase
Version:
Type-safe environment variable validation with Standard Schema compliance
30 lines (29 loc) • 1.2 kB
TypeScript
import type { MergeDeep, SimplifyDeep } from 'type-fest';
import type { StandardSchemaV1 } from './standard-schema.ts';
export type NodeEnvInfo = {
isProduction: boolean;
isTest: boolean;
isDevelopment: boolean;
};
export type EnvvarEntry<T extends StandardSchemaV1> = [string, T];
export type EnvSchema = {
[key: string]: EnvSchema | EnvvarEntry<StandardSchemaV1>;
};
type RecursiveInferEnv<T extends EnvSchema> = {
[K in keyof T]: T[K] extends EnvvarEntry<infer Schema> ? StandardSchemaV1.InferOutput<Schema> : T[K] extends EnvSchema ? RecursiveInferEnv<T[K]> : never;
};
export type InferEnv<T extends EnvSchema> = SimplifyDeep<RecursiveInferEnv<T>>;
export type EnvvarValidationIssue = {
name: string;
value?: string;
messages: string[];
};
type ComputedResolver = (raw: any) => unknown;
export type ComputedSchema<TRaw> = {
[key: string]: ((raw: TRaw) => unknown) | ComputedSchema<TRaw>;
};
export type InferComputed<T> = {
[K in keyof T]: T[K] extends ComputedResolver ? ReturnType<T[K]> : T[K] extends object ? InferComputed<T[K]> : never;
};
export type InferConfig<TRaw, TComputed> = SimplifyDeep<MergeDeep<TRaw, InferComputed<TComputed>>>;
export {};