首頁 >web前端 >js教程 >Tnv原始碼中的EnvOptions類型解釋

Tnv原始碼中的EnvOptions類型解釋

Susan Sarandon
Susan Sarandon原創
2024-11-14 20:43:02622瀏覽

在本文中,我們將研究 T3 Env 原始碼中的 EnvOptions 類型。如果您想知道 T3 Env 或 EnvOptions 是什麼,

T3 Env 使用 zod 提供型別安全環境變數的驗證。您使用 createEnv 函數並為您的伺服器和用戶端環境變數提供 zod 驗證,如下例所示。

// src/env.mjs
import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";
export const env = createEnv({
 /*
 * Serverside Environment variables, not available on the client.
 * Will throw if you access these variables on the client.
 */
 server: {
 DATABASE_URL: z.string().url(),
 OPEN_AI_API_KEY: z.string().min(1),
 },
 /*
 * Environment variables available on the client (and server).
 *
 * ? You'll get type errors if these are not prefixed with NEXT_PUBLIC_.
 */
 client: {
 NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),
 },
 /*
 * Due to how Next.js bundles environment variables on Edge and Client,
 * we need to manually destructure them to make sure all are included in bundle.
 *
 * ? You'll get type errors if not all variables from `server` & `client` are included here.
 */
 runtimeEnv: {
 DATABASE_URL: process.env.DATABASE_URL,
 OPEN_AI_API_KEY: process.env.OPEN_AI_API_KEY,
 NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY:
 process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,
 },
});

EnvOptions type in Tnv source code explained

此 createEnv 具有 T3 Env 原始碼中定義的以下類型:

export function createEnv<
 TPrefix extends TPrefixFormat,
 TServer extends TServerFormat = NonNullable<unknown>,
 TClient extends TClientFormat = NonNullable<unknown>,
 TShared extends TSharedFormat = NonNullable<unknown>,
 const TExtends extends TExtendsFormat = [],
>(
 opts: EnvOptions<TPrefix, TServer, TClient, TShared, TExtends>,
): CreateEnv<TServer, TClient, TShared, TExtends> {
 const runtimeEnv = opts.runtimeEnvStrict ?? opts.runtimeEnv ?? process.env;

這裡的函數參數是類型為EnvOptions

的opts

EnvOptions type in Tnv source code explained

export type EnvOptions<
 TPrefix extends string | undefined,
 TServer extends Record<string, ZodType>,
 TClient extends Record<string, ZodType>,
 TShared extends Record<string, ZodType>,
 TExtends extends Array<Record<string, unknown>>,
> =
 | (LooseOptions<TShared, TExtends> &
 ServerClientOptions<TPrefix, TServer, TClient>)
 | (StrictOptions<TPrefix, TServer, TClient, TShared, TExtends> &
 ServerClientOptions<TPrefix, TServer, TClient>);

EnvOptions 是一個泛型類型。傳遞的 opts 中的伺服器物件具有通用類型 - TServer extends Record

// server type is TServer that is a Record with key being string 
// and value being ZodType
server: {
 DATABASE_URL: z.string().url(),
 OPEN_AI_API_KEY: z.string().min(1),
},
// client type is TClient that is a Record with key being string 
// and value being ZodType
client: {
 NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),
},

我在這裡僅僅觸及了表面,這感覺就像一個高級的 Typescript 用例,因為此 En​​vOptions 類型返回:

| (LooseOptions<TShared, TExtends> &
 ServerClientOptions<TPrefix, TServer, TClient>)
| (StrictOptions<TPrefix, TServer, TClient, TShared, TExtends> &
 ServerClientOptions<TPrefix, TServer, TClient>);

查看 t3-env 原始碼中的 LooseOptions 和 ServerClientOptions。

關於我們:

在 Thinkthroo,我們研究大型開源專案並提供架構指南。我們開發了使用 Tailwind 建構的可重複使用元件,您可以在專案中使用它們。我們提供 Next.js、React 和 Node 開發服務。

與我們預約會面討論您的專案。

EnvOptions type in Tnv source code explained

參考文獻:

1. https://github.com/t3-oss/t3-env/blob/main/packages/core/src/index.ts#L222

2. https://github.com/t3-oss/t3-env/blob/main/packages/core/src/index.ts#L183

3. https://env.t3.gg/

以上是Tnv原始碼中的EnvOptions類型解釋的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn