Skip to content

Pick of enum keys #33308

@codercodingthecode

Description

@codercodingthecode

I came across a functionality I needed and I couldn't really find a solution out there.
Interfaces have an easy Pick functionality. However, enums and types don't

This is what I had to do to Pick the keys out of an enum. [Which I used in a select/dropdown]
It would be nice to have a pick like that build in.

perhaps something like this:

enum KeysToBePickedFrom {
    KEY_ONE = "Key One",
    KEY_TWO = "Key Number Two",
    KEY_THREE = "Another key n. 3",
    LAST_KEY= "Here is the last Key"
}

// desired pick functionality
interface Picked {
    y: Pick<keyof KeysToBePickedFrom, 'KEY_ONE', 'LAST_KEY'>
}

const z: Picked = {
    y: "KEY_ONE"     //  KEY_ONE | LAST_KEY
}

current workaround implementation:

enum KeysToBePickedFrom {
    KEY_ONE = "Key One",
    KEY_TWO = "Key Number Two",
    KEY_THREE = "Another key n. 3",
    LAST_KEY= "Here is the last Key"
}

type KeysOfEnum_KeysToBePickedFrom = {[key in keyof typeof KeysToBePickedFrom]: string }

type Picked_KeysOfEnum = Pick<KeysOfEnum_KeysToBePickedFrom, 'KEY_ONE' | 'LAST_KEY' >

interface KeysPickedForType {
    keyone: Picked_KeysOfEnum
}

const picks: KeysPickedForType = {
    keyone: "KEY_ONE"   //  KEY_ONE | LAST_KEY
}

Anyone with a better approach or a point to a built-in functionality in the docs?
Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions