#What do I specify if I intend to accept an Enum as a parameter/type parameter?

1 messages · Page 1 of 1 (latest)

ornate crystalBOT
#
nop#2745

Preview:```ts
enum Perm {
Read = 0b01,
Write = 0b11,
}

class EnumWrapper<
O extends Record<string, number> &
Record<number, string>

{
constructor(public o: O) {
//
}

toNumber(p: string | number): number {
if (typeof p === number) {
return
...```

untold bison
#

Record<string, number> & Record<number, string>

urban mica
#

!closed

ornate crystalBOT
#

@untold bison
Because your issue seemed to be resolved, this post was marked as resolved by @urban mica.
If your issue is not resolved, you can reopen this post by running !reopen.
If you have a different question, make a new post in #1057653400046674112.

untold bison
#

@urban micawhat is Enum's type?

urban mica
#

@untold bison Oh, I closed the thread because the original message was deleted and thought your message was an answer.

#

!reopen

untold bison
#

no worries

sterile ruin
#

the issue here is that number index signatures are actually string index signatures too

#

so idk if there's a way to solve this perfectly

untold bison
#

whats the type for enums when its a numeric enum

#

I intend users to pass in a numeric enum

#

since it does bit operations

sterile ruin
#

the type is basically right

#

it's just that the string index signature must also contain the number index signature

ornate crystalBOT
sterile ruin
#

!ts

ornate crystalBOT
#
// 8<
enum Perm {
  Read = 0b01,
  Write = 0b11,
}


class EnumWrapper<O extends Record<string, number | string> & Record<number, string>> {
  constructor(public o: O) {
    //
  }

  toNumber(p: string | number): number {
    if (typeof p === `number`) {
      return p;
    }
    return this.o[p] as number;
  }
}

const p = new EnumWrapper(Perm)
untold bison
#

whtas 8< btw? i see you putting that alot

untold bison
#

so, Record<string, number | string> & Record<number, string> is the correct type?

#

also, why doesn't the typescript handbook explain this

lilac sun
#
enum Perm {
  Read = 0b01,
  Write = 0b11,
}


class EnumWrapper<O extends Record<string, any>> {
  constructor(public o: O) {}

  toNumber(p: string | number): number {
    if (typeof p === `number`) {
      return p;
    }
    return this.o[p];
  }
}

const p = new EnumWrapper(Perm)
sterile ruin
untold bison
#

looks cool

untold bison
sterile ruin
#

i (sometimes) select it before copying if i want to !ts

untold bison
#

does !ts recognize // 8<

sterile ruin
#

idk why, i guess i just think !ts output looks better sometimes

sterile ruin
sterile ruin
lilac sun
#

Enum process to tsc will be compiled to object, so it's good to use Record<string, any>

untold bison
sterile ruin
#

but that's hard to constrain

#

wait

lilac sun
untold bison
lilac sun
#

no real type when using the function

ornate crystalBOT
sterile ruin
#

!ts

ornate crystalBOT
#
// 8<
enum Perm {
  Read = 0b01,
  Write = 0b11,
}

class EnumWrapper<O extends Record<string, number | string> & Record<number, string>> {
  constructor(public o: O, private get_: (k: keyof O) => number) {
    //
  }

  toNumber(p: string | number): number {
    if (typeof p === `number`) {
      return p;
    }
    return this.get_(p);
  }
}

const p = new EnumWrapper(Perm, k => Perm[k]);
sterile ruin
#

no type errors :D

#

of course, the issue is that you'd have to type out the function every time

ornate crystalBOT
#
n_n#2622

Preview:ts ... enum EmptyEnum {} class EnumWrapper<O extends typeof EmptyEnum> { const ...

sterile ruin
#

wait

#

oh no

sterile ruin
#

very mysterious

sterile ruin
lilac sun
#

enum is a literal object

urban mica
#

@sterile ruin FYI you can now 'escape' playground ulrs with < > to omit the preview entirely (e.g. if you're already pasting the code into the chat)

ornate crystalBOT
sterile ruin
#

oh

#

very cool

sterile ruin
#

yea

untold bison
#

ok