在本文中,我们将研究 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, }, });
此 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
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 用例,因为此 EnvOptions 类型返回:
| (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 开发服务。
与我们预约会面讨论您的项目。
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中文网其他相关文章!