Skip to content

Commit

Permalink
don't restrict to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed May 6, 2023
1 parent 979cd9a commit a50e929
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/gentle-ears-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@t3-oss/env-core": minor
---

don't restrict runtimeEnv to just strings

turns out some frameworks inject environment variables as booleans or numbers, this allows you to use those without getting type errors
4 changes: 2 additions & 2 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export interface LooseOptions<
* Runtime Environment variables to use for validation - `process.env`, `import.meta.env` or similar.
* Unlike `runtimeEnvStrict`, this doesn't enforce that all environment variables are set.
*/
runtimeEnv: Record<string, string | undefined>;
runtimeEnv: Record<string, string | boolean | number | undefined>;
}

export interface StrictOptions<
Expand All @@ -88,7 +88,7 @@ export interface StrictOptions<
: never;
}[keyof TClient]
| keyof TServer,
string | undefined
string | boolean | number | undefined
>;
runtimeEnv?: never;
}
Expand Down
25 changes: 25 additions & 0 deletions packages/core/test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,31 @@ describe("return type is correctly inferred", () => {
});
});

test("can pass number and booleans", () => {
const env = createEnv({
clientPrefix: "FOO_",
server: {
PORT: z.number(),
IS_DEV: z.boolean(),
},
client: {},
runtimeEnvStrict: {
PORT: 123,
IS_DEV: true,
},
});

expectTypeOf(env).toEqualTypeOf<{
PORT: number;
IS_DEV: boolean;
}>();

expect(env).toMatchObject({
PORT: 123,
IS_DEV: true,
});
});

describe("errors when validation fails", () => {
test("envs are missing", () => {
expect(() =>
Expand Down

1 comment on commit a50e929

@vercel
Copy link

@vercel vercel bot commented on a50e929 May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

t3-env – ./

t3-env.vercel.app
t3-env-t3-oss.vercel.app
t3-env-git-main-t3-oss.vercel.app
env.t3.gg

Please sign in to comment.