Skip to content

Change spec types to allow ReadonlyArray choices#112

Merged
af merged 1 commit intoaf:masterfrom
beaulac:types/readonly-choices
Dec 18, 2019
Merged

Change spec types to allow ReadonlyArray choices#112
af merged 1 commit intoaf:masterfrom
beaulac:types/readonly-choices

Conversation

@beaulac
Copy link
Contributor

@beaulac beaulac commented Dec 16, 2019

In TypeScript, the current type definitions don't allow the following:

const myChoices: ReadonlyArray<string> = ['foo', 'bar'];
const myEnv = cleanEnv(process.env, { MY_ENV_VAR: str({choices: myChoices});

// Error:(...) TS4104: The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'any[]'.

Given that the choices field doesn't need to be modified (only the include method is used currently), it should be allowed to be a ReadonlyArray. Generally in TS, it’s good practice to use ReadonlyArray over Array when no mutation is intended.

Any Array<T> is also assignable to ReadonlyArray<T>, so this change doesn't break any existing behaviour.

This change uses ReadonlyArray<T> instead of readonly T[] to maintain compatibility with TS <3.x.

@af af merged commit 60bab04 into af:master Dec 18, 2019
@af
Copy link
Owner

af commented Dec 18, 2019

Looks good to me, thanks!

tuannm151 pushed a commit to BSSCommerce/shopify-envalid that referenced this pull request Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants