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

Map choices array to union types #107

Open
Nicholaiii opened this issue Jan 15, 2022 · 2 comments
Open

Map choices array to union types #107

Nicholaiii opened this issue Jan 15, 2022 · 2 comments
Labels
wontfix This will not be worked on

Comments

@Nicholaiii
Copy link

This is a great QoL addition that I've been using a lot from ts-dotenv, but the implementation details of this library is so different that another approach is better.
In ts-dotenv you specify an array that are cast as const as so:

LOG_LEVEL: {
    type: [
      'fatal' as const,
      'error' as const,
      'warn' as const,
      'info' as const,
      'debug' as const,
      'trace' as const
    ],
    default: 'info'
  },

which gives you the type

'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'

It would be amazing if we could conditionally do this for choices. Here is an example of how it can be done:

function stringLiteralArray<T extends string>(a: T[]) {
    return a;
}

const fruits = stringLiteralArray(["Apple", "Orange", "Pear"]);
type Fruits = typeof fruits[number]
@KATT
Copy link
Owner

KATT commented Jan 15, 2022

Agreed! Would be nice.

@KATT
Copy link
Owner

KATT commented Apr 8, 2022

I've actually started using zod for env vars instead of my own package as I have it installed anyway. See here for an example:

@KATT KATT added the wontfix This will not be worked on label Jun 19, 2022
Repository owner locked and limited conversation to collaborators Jan 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants