Basic Conditionals

Basic Conditionals
πŸ‘¨β€πŸ’Ό Let's create conditional types that check and transform based on type conditions.
type IsNumber<T> = T extends number ? true : false
type UnwrapArray<T> = T extends Array<infer U> ? U : T

type A = IsNumber<42> // true
type B = UnwrapArray<Array<'x'>> // 'x'
🐨 Open
index.ts
and:
  1. Create these conditional types:
    • IsString<T> β†’ true / false
    • IsArray<T> β†’ true / false
    • IsFunction<T> β†’ true / false
    • Flatten<T> β†’ element type when T is an array, otherwise T
    • MyNonNullable<T> β†’ removes null and undefined
  2. Implement a runtime helper process(value) that:
    • Returns the first element when value is an array
    • Returns value unchanged otherwise
    • Uses Flatten for its return type
  3. Export process
Completion checks:
  • process([1, 2, 3]) β†’ 1
  • process(42) β†’ 42
  • process(['a', 'b', 'c']) β†’ 'a'
  • process(100) β†’ 100

Please set the playground first

Loading "Basic Conditionals"
Loading "Basic Conditionals"
Login to get access to the exclusive discord channel.
Loading Discord Posts