ホームページ >ウェブフロントエンド >jsチュートリアル >Tnv ソース コードの EnvOptions タイプの説明

Tnv ソース コードの EnvOptions タイプの説明

Susan Sarandon
Susan Sarandonオリジナル
2024-11-14 20:43:02660ブラウズ

この記事では、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 は 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),
},

ここではほんの表面をなぞっただけですが、この EnvOptions 型が返すように、これは高度な Typescript のユースケースのように感じられます。

| (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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。