@@ -106,10 +106,12 @@ describe("return type is correctly inferred", () => {
106
106
} ,
107
107
} ) ;
108
108
109
- expectTypeOf ( env ) . toEqualTypeOf < {
110
- BAR : string ;
111
- FOO_BAR : string ;
112
- } > ( ) ;
109
+ expectTypeOf ( env ) . toEqualTypeOf <
110
+ Readonly < {
111
+ BAR : string ;
112
+ FOO_BAR : string ;
113
+ } >
114
+ > ( ) ;
113
115
114
116
expect ( env ) . toMatchObject ( {
115
117
BAR : "bar" ,
@@ -128,10 +130,12 @@ describe("return type is correctly inferred", () => {
128
130
} ,
129
131
} ) ;
130
132
131
- expectTypeOf ( env ) . toEqualTypeOf < {
132
- BAR : number ;
133
- FOO_BAR : string ;
134
- } > ( ) ;
133
+ expectTypeOf ( env ) . toEqualTypeOf <
134
+ Readonly < {
135
+ BAR : number ;
136
+ FOO_BAR : string ;
137
+ } >
138
+ > ( ) ;
135
139
136
140
expect ( env ) . toMatchObject ( {
137
141
BAR : 123 ,
@@ -149,9 +153,11 @@ describe("return type is correctly inferred", () => {
149
153
} ,
150
154
} ) ;
151
155
152
- expectTypeOf ( env ) . toEqualTypeOf < {
153
- BAR : string ;
154
- } > ( ) ;
156
+ expectTypeOf ( env ) . toEqualTypeOf <
157
+ Readonly < {
158
+ BAR : string ;
159
+ } >
160
+ > ( ) ;
155
161
156
162
expect ( env ) . toMatchObject ( {
157
163
BAR : "bar" ,
@@ -173,10 +179,12 @@ test("can pass number and booleans", () => {
173
179
} ,
174
180
} ) ;
175
181
176
- expectTypeOf ( env ) . toEqualTypeOf < {
177
- PORT : number ;
178
- IS_DEV : boolean ;
179
- } > ( ) ;
182
+ expectTypeOf ( env ) . toEqualTypeOf <
183
+ Readonly < {
184
+ PORT : number ;
185
+ IS_DEV : boolean ;
186
+ } >
187
+ > ( ) ;
180
188
181
189
expect ( env ) . toMatchObject ( {
182
190
PORT : 123 ,
@@ -280,7 +288,7 @@ describe("client/server only mode", () => {
280
288
runtimeEnv : { FOO_BAR : "foo" } ,
281
289
} ) ;
282
290
283
- expectTypeOf ( env ) . toEqualTypeOf < { FOO_BAR : string } > ( ) ;
291
+ expectTypeOf ( env ) . toEqualTypeOf < Readonly < { FOO_BAR : string } > > ( ) ;
284
292
expect ( env ) . toMatchObject ( { FOO_BAR : "foo" } ) ;
285
293
} ) ;
286
294
@@ -292,7 +300,7 @@ describe("client/server only mode", () => {
292
300
runtimeEnv : { BAR : "bar" } ,
293
301
} ) ;
294
302
295
- expectTypeOf ( env ) . toEqualTypeOf < { BAR : string } > ( ) ;
303
+ expectTypeOf ( env ) . toEqualTypeOf < Readonly < { BAR : string } > > ( ) ;
296
304
expect ( env ) . toMatchObject ( { BAR : "bar" } ) ;
297
305
} ) ;
298
306
@@ -336,11 +344,13 @@ describe("shared can be accessed on both server and client", () => {
336
344
runtimeEnv : process . env ,
337
345
} ) ;
338
346
339
- expectTypeOf ( env ) . toEqualTypeOf < {
340
- NODE_ENV : "development" | "production" | "test" ;
341
- BAR : string ;
342
- FOO_BAR : string ;
343
- } > ( ) ;
347
+ expectTypeOf ( env ) . toEqualTypeOf <
348
+ Readonly < {
349
+ NODE_ENV : "development" | "production" | "test" ;
350
+ BAR : string ;
351
+ FOO_BAR : string ;
352
+ } >
353
+ > ( ) ;
344
354
345
355
test ( "server" , ( ) => {
346
356
const { window } = globalThis ;
@@ -369,3 +379,28 @@ describe("shared can be accessed on both server and client", () => {
369
379
globalThis . window = window ;
370
380
} ) ;
371
381
} ) ;
382
+
383
+ test ( "envs are readonly" , ( ) => {
384
+ const env = createEnv ( {
385
+ server : { BAR : z . string ( ) } ,
386
+ runtimeEnv : { BAR : "bar" } ,
387
+ } ) ;
388
+
389
+ /**
390
+ * We currently don't enforce readonly during runtime:
391
+ * https://github.com/t3-oss/t3-env/pull/111#issuecomment-1682931526
392
+ */
393
+
394
+ // expect(() => {
395
+ // // @ts -expect-error - envs are readonly
396
+ // env.BAR = "foo";
397
+ // }).toThrowErrorMatchingInlineSnapshot(
398
+ // '"Cannot assign to read only property BAR of object #<Object>"'
399
+ // );
400
+
401
+ // expect(env).toMatchObject({ BAR: "bar" });
402
+
403
+ // @ts -expect-error - envs are readonly
404
+ env . BAR = "foo" ;
405
+ expect ( env ) . toMatchObject ( { BAR : "foo" } ) ;
406
+ } ) ;
0 commit comments