cf.completefrontend

ConceptHard

Can a type predicate lie? What's wrong with this isNonEmptyString guard?

The code
function isNonEmptyString(value: unknown): value is string {  return typeof value === "string" && value.length > 0;} function describe(value: string | number) {  if (isNonEmptyString(value)) {    return value.toUpperCase();  }  return value.toFixed(2);} describe("");

The answer

Answer it out loud first, the way you would in the interview. Then compare.

Checking your sign-in…