Typescript: Pick of enum keys

Created on 8 Sep 2019  ·  7Comments  ·  Source: microsoft/TypeScript

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?

Question

Most helpful comment

Just a reminder that this is a bug tracking system, not a support forum.

All 7 comments

Your workaround does not do what you think it does.

Have you tried wrapping Extract<>?

@AnyhowStep , it does exactly what I think it does, provides me with propper types on that property according to whatever enums I throw on my select/dropdown component with a reverse mapping on the parent enum.
However, I'm looking for suggestions though for a better approach.

Your workaround does not do what you think it does.

image


This is closer to what you want,

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 PickKey<T, K extends keyof T> = Extract<keyof T, K>;

type Picked_KeysOfEnum = PickKey<typeof KeysToBePickedFrom, 'KEY_ONE' | 'LAST_KEY' >

interface KeysPickedForType {
    keyone: Picked_KeysOfEnum
}

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

Playground

And the result,

image


For help with code, consider StackOverflow or the TypeScript Gitter

Just a reminder that this is a bug tracking system, not a support forum.

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

It looks like I found a kind of solution.

export enum ECandleTimeFrame {
  S1  = 'S1',
  S3  = 'S3',
  S5  = 'S5',
  S15 = 'S15',
  S30 = 'S30',
  S45 = 'S45',

  M1  = 'M1',
  M3  = 'M3',
  M5  = 'M5',
  M7  = 'M7',
  M15 = 'M15',
  M30 = 'M30',
  M45 = 'M45',

  H1  = 'H1',
  H2  = 'H2',
  H3  = 'H3',
  H4  = 'H4',
  H6  = 'H6',
  H12 = 'H12',

  D1  = 'D1',
  W1  = 'W1',
  N1  = 'N1', /* Month 1 */
  Y1  = 'Y1',
}

export const EKunaCandleTimeFrame
  = _.pick(ECandleTimeFrame, ['M1', 'M5', 'M15', 'M30', 'H1', 'H2', 'H4', 'H6', 'D1', 'W1']);

export type EKunaCandleTimeFrame = keyof typeof EKunaCandleTimeFrame;

const a: EKunaCandleTimeFrame = EKunaCandleTimeFrame.M1; // valid
const b: EKunaCandleTimeFrame = ECandleTimeFrame.M1;     // valid
const c: EKunaCandleTimeFrame = ECandleTimeFrame.S1;     // error

However, it works only in case when values equal their keys.

StackBlitz Live Demo

you'll want the values instead:

type ValuesOf<T> = T[keyof T]
export type EKunaCandleTimeFrame = ValuesOf<typeof EKunaCandleTimeFrame>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielRosenwasser picture DanielRosenwasser  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

wmaurer picture wmaurer  ·  3Comments

siddjain picture siddjain  ·  3Comments

manekinekko picture manekinekko  ·  3Comments