ApplicationInsights-node.js icon indicating copy to clipboard operation
ApplicationInsights-node.js copied to clipboard

Contracts.SeverityLevel should not be an enum

Open devinrhode2 opened this issue 3 years ago • 4 comments

ApplicationInsights library code uses an enum here: node_modules/applicationinsights/out/Declarations/Contracts/Generated/SeverityLevel.d.t

/**
 * Defines the level of severity for the event.
 */
declare enum SeverityLevel {
    Verbose = 0,
    Information = 1,
    Warning = 2,
    Error = 3,
    Critical = 4
}
export = SeverityLevel;

I don't know if there's a particularly good reason to use an enum here, I'd prefer this to be a const.

Here's my (wip) "user-land" code

type SeverityLevel =
  import('applicationinsights').Contracts.SeverityLevel

// IDEALLY: derive this union from import('applicationinsights').Contracts.SeverityLevel
// ...Appears to not be possible: https://stackoverflow.com/questions/63758383/how-do-i-a-union-type-of-string-values-from-an-enum
type SimpleStrictAiSeverityLevelUnion =
  | 0
  | 1
  | 2
  | 3
  | 4

let a: SeverityLevel = 999999

https://stackoverflow.com/questions/63758383/how-do-i-a-union-type-of-string-values-from-an-enum

Q: Is there a way to use this enum on my last line, let a = 9999, which will cause this to be a type error?

There is a similar issue, but it seems to be focused on getting the enum values at runtime: https://github.com/microsoft/ApplicationInsights-node.js/issues/55

devinrhode2 avatar Apr 06 '22 14:04 devinrhode2

Probably we should just move away from using enums... this is not even a type-error: CleanShot 2022-04-06 at 09 40 14@2x

SL has type string

devinrhode2 avatar Apr 06 '22 14:04 devinrhode2

I tried a different google search "typescript get union of values of an enum" and found https://stackoverflow.com/questions/52393730/typescript-string-literal-union-type-from-enum

So now I can get "0" | "1" | "2" | "3" | "4" which is nice but they are all strings

Converting to numerics would require me to create a map anyway.. https://stackoverflow.com/questions/70526617/typescript-is-there-a-way-to-convert-a-string-literal-type-to-a-number-type

devinrhode2 avatar Apr 06 '22 14:04 devinrhode2

I'm trying to avoid patching applicationinsights src, but it's becoming nonsense:

// Ideally: SeverityLevel is not an enum, see: https://github.com/microsoft/ApplicationInsights-node.js/issues/942
type SeverityLevelEnum =
  import('applicationinsights').Contracts.SeverityLevel

// "typescript get union of values of an enum" https://stackoverflow.com/questions/52393730/typescript-string-literal-union-type-from-enum
type SeverityLevelStringUnion =
  | `${SeverityLevelEnum}`
  | '5'

// "typescript convert string literal to numeric literal" https://stackoverflow.com/questions/70526617/typescript-is-there-a-way-to-convert-a-string-literal-type-to-a-number-type
type MapStrNum = {
  '0': 0
  '1': 1
  '2': 2
  '3': 3
  '4': 4
}

// Q: how do I use these type values to create a generic `0 | 1 | 2 | 3 | 4` ?

let someSL = 8 as const // My src uses numbers currently (i.e. the enum values)
let someSLString = `${someSL}` // has type `string` not "8"

devinrhode2 avatar Apr 06 '22 14:04 devinrhode2

This is what I'm going with for now:

// Ideally: SeverityLevel is not an enum, see: https://github.com/microsoft/ApplicationInsights-node.js/issues/942
export type strictAiSeverityLevel =
  | 0
  | 1
  | 2
  | 3
  | 4

devinrhode2 avatar Apr 07 '22 00:04 devinrhode2