Skip to content

Commit

Permalink
Fixed runtimeEnvStrict type infer for empty client config (#121)
Browse files Browse the repository at this point in the history
Co-authored-by: Julius Marminge <julius0216@outlook.com>
  • Loading branch information
chungweileong94 and juliusmarminge committed Oct 5, 2023
1 parent 86ea798 commit c77c240
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-birds-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@t3-oss/env-core": patch
---

Fixed type infer for empty client config
20 changes: 12 additions & 8 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface LooseOptions<TShared extends Record<string, ZodType>>
}

export interface StrictOptions<
TPrefix extends string,
TPrefix extends string | undefined,
TServer extends Record<string, ZodType>,
TClient extends Record<string, ZodType>,
TShared extends Record<string, ZodType>
Expand All @@ -67,12 +67,16 @@ export interface StrictOptions<
*/
runtimeEnvStrict: Record<
| {
[TKey in keyof TClient]: TKey extends `${TPrefix}${string}`
[TKey in keyof TClient]: TPrefix extends undefined
? never
: TKey extends `${TPrefix}${string}`
? TKey
: never;
}[keyof TClient]
| {
[TKey in keyof TServer]: TKey extends `${TPrefix}${string}`
[TKey in keyof TServer]: TPrefix extends undefined
? TKey
: TKey extends `${TPrefix}${string}`
? never
: TKey;
}[keyof TServer]
Expand All @@ -85,7 +89,7 @@ export interface StrictOptions<
}

export interface ClientOptions<
TPrefix extends string,
TPrefix extends string | undefined,
TClient extends Record<string, ZodType>
> {
/**
Expand All @@ -108,7 +112,7 @@ export interface ClientOptions<
}

export interface ServerOptions<
TPrefix extends string,
TPrefix extends string | undefined,
TServer extends Record<string, ZodType>
> {
/**
Expand Down Expand Up @@ -142,7 +146,7 @@ export interface ServerOptions<
}

export type ServerClientOptions<
TPrefix extends string,
TPrefix extends string | undefined,
TServer extends Record<string, ZodType>,
TClient extends Record<string, ZodType>
> =
Expand All @@ -151,7 +155,7 @@ export type ServerClientOptions<
| (ClientOptions<TPrefix, TClient> & Impossible<ServerOptions<never, never>>);

export type EnvOptions<
TPrefix extends string,
TPrefix extends string | undefined,
TServer extends Record<string, ZodType>,
TClient extends Record<string, ZodType>,
TShared extends Record<string, ZodType>
Expand All @@ -161,7 +165,7 @@ export type EnvOptions<
ServerClientOptions<TPrefix, TServer, TClient>);

export function createEnv<
TPrefix extends string = "",
TPrefix extends string | undefined,
TServer extends Record<string, ZodType> = NonNullable<unknown>,
TClient extends Record<string, ZodType> = NonNullable<unknown>,
TShared extends Record<string, ZodType> = NonNullable<unknown>
Expand Down
12 changes: 7 additions & 5 deletions packages/core/test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,14 @@ describe("client/server only mode", () => {

test("config with missing client", () => {
ignoreErrors(() => {
createEnv({
createEnv(
// @ts-expect-error - incomplete client config - client not present
clientPrefix: "FOO_",
server: {},
runtimeEnv: {},
});
{
clientPrefix: "FOO_",
server: {},
runtimeEnv: {},
}
);
});
});

Expand Down

2 comments on commit c77c240

@vercel
Copy link

@vercel vercel bot commented on c77c240 Oct 5, 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 – ./docs

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

@vercel
Copy link

@vercel vercel bot commented on c77c240 Oct 5, 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-nextjs – ./examples/nextjs

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

Please sign in to comment.