diff --git a/.changeset/polite-fireants-drive.md b/.changeset/polite-fireants-drive.md new file mode 100644 index 00000000..bcd31700 --- /dev/null +++ b/.changeset/polite-fireants-drive.md @@ -0,0 +1,7 @@ +--- +"@t3-oss/env-nextjs": patch +"@t3-oss/env-core": patch +"@t3-oss/env-nuxt": patch +--- + +refactor: add explicit return type to make lib more portable diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index f6337de8..72770be2 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,4 +1,5 @@ -import { type ZodError, type ZodObject, type ZodType, z } from "zod"; +import type { TypeOf, ZodError, ZodObject, ZodType } from "zod"; +import { object } from "zod"; export type ErrorMessage = T; export type Simplify = { @@ -191,22 +192,35 @@ export type EnvOptions< | (StrictOptions & ServerClientOptions); -export function createEnv< - TPrefix extends string | undefined, - TServer extends Record = NonNullable, - TClient extends Record = NonNullable, - TShared extends Record = NonNullable, - const TExtends extends Array> = [], ->( - opts: EnvOptions, -): Readonly< +type TPrefixFormat = string | undefined; +type TServerFormat = Record; +type TClientFormat = Record; +type TSharedFormat = Record; +type TExtendsFormat = Array>; + +export type CreateEnv< + TServer extends TServerFormat, + TClient extends TClientFormat, + TShared extends TSharedFormat, + TExtends extends TExtendsFormat, +> = Readonly< Simplify< - z.infer> & - z.infer> & - z.infer> & + TypeOf> & + TypeOf> & + TypeOf> & UnReadonlyObject> > -> { +>; + +export function createEnv< + TPrefix extends TPrefixFormat, + TServer extends TServerFormat = NonNullable, + TClient extends TClientFormat = NonNullable, + TShared extends TSharedFormat = NonNullable, + const TExtends extends TExtendsFormat = [], +>( + opts: EnvOptions, +): CreateEnv { const runtimeEnv = opts.runtimeEnvStrict ?? opts.runtimeEnv ?? process.env; const emptyStringAsUndefined = opts.emptyStringAsUndefined ?? false; @@ -225,9 +239,9 @@ export function createEnv< const _client = typeof opts.client === "object" ? opts.client : {}; const _server = typeof opts.server === "object" ? opts.server : {}; const _shared = typeof opts.shared === "object" ? opts.shared : {}; - const client = z.object(_client); - const server = z.object(_server); - const shared = z.object(_shared); + const client = object(_client); + const server = object(_server); + const shared = object(_shared); const isServer = opts.isServer ?? (typeof window === "undefined" || "Deno" in window); diff --git a/packages/nextjs/src/index.ts b/packages/nextjs/src/index.ts index 6fa131e6..e856c6fd 100644 --- a/packages/nextjs/src/index.ts +++ b/packages/nextjs/src/index.ts @@ -1,5 +1,5 @@ import type { ServerClientOptions, StrictOptions } from "@t3-oss/env-core"; -import { createEnv as createEnvCore } from "@t3-oss/env-core"; +import { type CreateEnv, createEnv as createEnvCore } from "@t3-oss/env-core"; import type { ZodType } from "zod"; const CLIENT_PREFIX = "NEXT_PUBLIC_" as const; @@ -57,7 +57,9 @@ export function createEnv< > = NonNullable, TShared extends Record = NonNullable, const TExtends extends Array> = [], ->(opts: Options) { +>( + opts: Options, +): CreateEnv { const client = typeof opts.client === "object" ? opts.client : {}; const server = typeof opts.server === "object" ? opts.server : {}; const shared = opts.shared; diff --git a/packages/nuxt/src/index.ts b/packages/nuxt/src/index.ts index 09a338c3..9fadb250 100644 --- a/packages/nuxt/src/index.ts +++ b/packages/nuxt/src/index.ts @@ -1,4 +1,4 @@ -import { createEnv as createEnvCore } from "@t3-oss/env-core"; +import { type CreateEnv, createEnv as createEnvCore } from "@t3-oss/env-core"; import type { ServerClientOptions, StrictOptions } from "@t3-oss/env-core"; import type { ZodType } from "zod"; @@ -18,10 +18,15 @@ type Options< export function createEnv< TServer extends Record = NonNullable, - TClient extends Record = NonNullable, + TClient extends Record< + `${ClientPrefix}${string}`, + ZodType + > = NonNullable, TShared extends Record = NonNullable, const TExtends extends Array> = [], ->(opts: Options) { +>( + opts: Options, +): CreateEnv { const client = typeof opts.client === "object" ? opts.client : {}; const server = typeof opts.server === "object" ? opts.server : {}; const shared = opts.shared;