t3-oss / t3-env

https://env.t3.gg
MIT License
2.64k stars 86 forks source link

Empty `extends` array introduces type error TS2339 #179

Open rydkvist opened 7 months ago

rydkvist commented 7 months ago

Background

Hi there! I'm having the following compile error TS2339 when I keep the extends key as an empty array.

The workaround I've found is simply removing the extends key, which of course makes sense if I'm not using it. But I wasn't expecting it to introduce compile errors for the environment variables for keeping the extends key around, for perhaps future use of it.

image

My environment config

const env = createEnv({
  client: {
    ...
  },
  server: {
    APP_ENV: z.string(),
    APP_NAME: z.string().min(1),
    ...
  },
  shared: {
    NODE_ENV: z.enum(['development', 'production']),
  },
  runtimeEnv: {
    APP_ENV: process.env.APP_ENV,
    APP_NAME: process.env.APP_NAME,
    ...
  },
  extends: [],
});

Relevant Package Versions

Project tsconfig settings:

{
  "compilerOptions": {
    "target": "ES2020",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "preserveConstEnums": true,
    "declaration": false,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      "@env": ["./env.ts"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}
juliusmarminge commented 7 months ago

don't see a real issue here since you can just remove the empty array if you're not extending anything. obviously open for a fix if anyone wants to spend time on it but i probably wont fix it myself tbh...

OrJDev commented 2 weeks ago

don't see a real issue here since you can just remove the empty array if you're not extending anything. obviously open for a fix if anyone wants to spend time on it but i probably wont fix it myself tbh...

looks like extends is just broken, not only for empty arrays but:

import { createEnv } from "@t3-oss/env-nextjs";
import { vercel } from "@t3-oss/env-nextjs/presets";
import { z } from "zod";

// env is never
export const env = createEnv({
  extends: [vercel()],
  shared: {
    NODE_ENV: z
      .enum(["development", "production", "test"])
      .default("development"),
  },
  /**
   * Specify your server-side environment variables schema here.
   * This way you can ensure the app isn't built with invalid env vars.
   */
  server: {
    DATABASE_URL: z
      .string()
      .url()
      .refine(
        (str) => !str.includes("YOUR_MYSQL_URL_HERE"),
        "You forgot to change the default URL",
      ),

    UPSTASH_REDIS_REST_TOKEN: z.string(),
    UPSTASH_REDIS_REST_URL: z.string(),
    TELEGRAM_TOKEN: z.string(),
  },

  /**
   * Specify your client-side environment variables schema here.
   * For them to be exposed to the client, prefix them with `NEXT_PUBLIC_`.
   */
  client: {
    // NEXT_PUBLIC_CLIENTVAR: z.string(),
    NEXT_PUBLIC_TEST_IP: z.string().optional(),
  },
  /**
   * Destructure all variables from `process.env` to make sure they aren't tree-shaken away.
   */
  runtimeEnv: {
    DATABASE_URL: process.env.DATABASE_URL,
    NODE_ENV: process.env.NODE_ENV,
    NEXT_PUBLIC_TEST_IP: process.env.NEXT_PUBLIC_TEST_IP,
    UPSTASH_REDIS_REST_TOKEN: process.env.UPSTASH_REDIS_REST_TOKEN,
    UPSTASH_REDIS_REST_URL: process.env.UPSTASH_REDIS_REST_URL,
    TELEGRAM_TOKEN: process.env.TELEGRAM_TOKEN,

    // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
  },
  skipValidation:
    !!process.env.CI || process.env.npm_lifecycle_event === "lint",
});
OrJDev commented 2 weeks ago

if i remove extends:

Screenshot 2024-08-28 at 22 48 26