JS Built-ins
TypeScriptbeginner

Type Guard β€” isNonEmptyString

πŸ“– Usage Example
function isNonEmptyString(x: unknown): x is string {
  return typeof x === 'string' && x.length > 0
}
isNonEmptyString('')        // β†’ false
isNonEmptyString('hello')   // β†’ true

Layering value-level constraints onto typeof checks creates more specific type guards.

Type Guards: Refined String Guard

Challenge: Implement isNonEmptyString(x: unknown): x is string β€” true only for non-empty strings.

isNonEmptyString('hello')  // β†’ true
isNonEmptyString('')       // β†’ false
isNonEmptyString(null)     // β†’ false
πŸ’‘ Hints (1)
  • β€’ Check typeof and length > 0.