Read Typed Values from Form Fields

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Form fields are a union told apart by type: text (a name and a string value), number (a name and a number value) and checkbox (a name and a checked flag). Export the Field type, readValue(field), which returns the field's value (checked for a checkbox), and readForm(fields), which returns an object mapping each field's name to its value. Type readValue so its result matches the field's kind (boolean for a checkbox, say), and readForm so the object has exactly the fields' names as keys, each with its own value type, from a list written inline; a field of an unknown type is a type error.

Your code must pass

  • Reads each kind of field
  • Reads a form into an object by field name
  • Keeps unticked checkboxes, zeros and empty text
  • Has no type errors
  • readValue returns the field's value type
  • readForm keys each value by its field's name
  • Rejects a field of an unknown type