ts.data.json - v4.1.0
    Preparing search index...

    Function enumeration

    • Decoder for enumeration values.

      Type Parameters

      • E

      Parameters

      • enumObj: object

        The enum object to use for decoding. Must not be a const enum.

      Returns Decoder<E>

      A decoder that validates and returns enum values

      enum Color {
      Red = 'red',
      Blue = 'blue'
      }

      const colorDecoder = JsonDecoder.enumeration(Color);
      colorDecoder.decode('red'); // Ok<Color>
      // The rejected value is rendered with JSON.stringify, so a string keeps its quotes...
      colorDecoder.decode('green'); // Err with issues: [{ message: '"green" is not a valid enum value', path: [] }]
      // ...while a non-string value (here a number) is rendered without quotes.
      colorDecoder.decode(42); // Err with issues: [{ message: '42 is not a valid enum value', path: [] }]