Drop Null and Undefined Fields, Typed

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

The problem

  • medium
  • Code

Write compact(obj): return a new object without the properties whose value is null or undefined, keeping falsy values like 0, empty strings and false. Export a type NonNullableFields<T> for its result: a field that could be null or undefined becomes optional and loses null, while every other field stays required with its own type. So reading a required field works as before, but a nullable one has to be checked first.

Your code must pass

  • Removes null and undefined values
  • Keeps falsy values like 0, empty strings and false
  • Leaves obj unchanged
  • Has no type errors
  • Nullable fields become optional and non-null
  • Other fields stay required