1
- /* eslint-disable @typescript-eslint/ban-ts-comment */
2
1
import { z } from "zod" ;
3
2
4
3
/**
@@ -32,16 +31,21 @@ const processEnv = {
32
31
// --------------------------
33
32
34
33
const merged = server . merge ( client ) ;
35
- /** @type z.infer<merged>
36
- * @ts -ignore - can't type this properly in jsdoc */
37
- let env = process . env ;
34
+
35
+ /** @typedef {z.input<typeof merged> } MergedInput */
36
+ /** @typedef {z.infer<typeof merged> } MergedOutput */
37
+ /** @typedef {z.SafeParseReturnType<MergedInput, MergedOutput> } MergedSafeParseReturn */
38
+
39
+ let env = /** @type {MergedOutput } */ ( process . env ) ;
38
40
39
41
if ( ! ! process . env . SKIP_ENV_VALIDATION == false ) {
40
42
const isServer = typeof window === "undefined" ;
41
43
42
- const parsed = isServer
43
- ? merged . safeParse ( processEnv ) // on server we can validate all env vars
44
- : client . safeParse ( processEnv ) ; // on client we can only validate the ones that are exposed
44
+ const parsed = /** @type {MergedSafeParseReturn } */ (
45
+ isServer
46
+ ? merged . safeParse ( processEnv ) // on server we can validate all env vars
47
+ : client . safeParse ( processEnv ) // on client we can only validate the ones that are exposed
48
+ ) ;
45
49
46
50
if ( parsed . success === false ) {
47
51
console . error (
@@ -51,8 +55,6 @@ if (!!process.env.SKIP_ENV_VALIDATION == false) {
51
55
throw new Error ( "Invalid environment variables" ) ;
52
56
}
53
57
54
- /** @type z.infer<merged>
55
- * @ts -ignore - can't type this properly in jsdoc */
56
58
env = new Proxy ( parsed . data , {
57
59
get ( target , prop ) {
58
60
if ( typeof prop !== "string" ) return undefined ;
@@ -64,8 +66,7 @@ if (!!process.env.SKIP_ENV_VALIDATION == false) {
64
66
? "❌ Attempted to access a server-side environment variable on the client"
65
67
: `❌ Attempted to access server-side environment variable '${ prop } ' on the client` ,
66
68
) ;
67
- /* @ts -ignore - can't type this properly in jsdoc */
68
- return target [ prop ] ;
69
+ return target [ /** @type {keyof typeof target } */ ( prop ) ] ;
69
70
} ,
70
71
} ) ;
71
72
}
0 commit comments