Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The type 'DeepReadonlyArray<string>' is 'readonly' and cannot be assigned to the mutable type #7

Closed
evantill opened this issue Apr 8, 2023 · 2 comments

Comments

@evantill
Copy link

evantill commented Apr 8, 2023

Can not use an array in the schema: Error TS2322 on parseEnv_

Code to reproduce the error

import {z} from 'zod'
import {parseEnv} from 'znv'

const envSchema = {
  OWNERS: z.string().array().nonempty().optional(),
}

const envObject = z.object(envSchema)
export type Environment = z.infer<typeof envObject>

export const loadEnvironment = (env = process.env): Environment => parseEnv(env, envSchema)

Stacktrace

   Test suite failed to run

    src/env.ts:18:68 - error TS2322: Type 'DeepReadonlyObject<{ EVENT_ACTION: string | undefined; GITHUB_EVENT_NAME: string; REF: string; REF_TYPE: "tag" | "branch" | undefined; ACTOR: string; GITHUB_REPOSITORY_OWNER: string; OWNERS: [string, ...string[]] | undefined; }>' is not assignable to type '{ GITHUB_EVENT_NAME: string; REF: string; ACTOR: string; GITHUB_REPOSITORY_OWNER: string; EVENT_ACTION?: string | undefined; REF_TYPE?: "tag" | "branch" | undefined; OWNERS?: [string, ...string[]] | undefined; }'.
      Types of property 'OWNERS' are incompatible.
        Type 'DeepReadonlyArray<string> | undefined' is not assignable to type '[string, ...string[]] | undefined'.
          The type 'DeepReadonlyArray<string>' is 'readonly' and cannot be assigned to the mutable type '[string, ...string[]]'.

    18 export const loadEnvironment = (env = process.env): Environment => parseEnv(env, envSchema)
                                                                          ~~~~~~~~~~~~~~~~~~~~~~~~
@lostfictions
Copy link
Owner

Hi there, this tends to be a limitation of TypeScript inference -- you'll often find you either need to declare things inline to make it work, or else add explicit annotations (possibly including as const). This tends to be true for other tooling that leans heavily on TypeScript inference like tRPC, so it's not exclusive to zod or znv.

In general when you encounter problems like these I'd recommend removing as many explicit annotations as possible and letting the inference do the work for you.

This works for me:

import { parseEnv, z } from "znv";

export const loadEnvironment = (env = process.env) =>
  parseEnv(env, {
    OWNERS: z.string().array().nonempty().optional()
  });

export type Environment = ReturnType<typeof loadEnvironment>  

CodeSandbox link.

ReturnType is a built-in TypeScript utility type. But as an aside, by the same principle of making sure an explicit annotation isn't just getting in the way of letting TypeScript infer things automatically, you might find you don't actually need an explicit Environment type, depending on usage -- using ReturnType can sometimes be an antipattern.

@evantill
Copy link
Author

Thank you for this great explanation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants