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
and:
- Create these conditional types:
IsString<T>βtrue/falseIsArray<T>βtrue/falseIsFunction<T>βtrue/falseFlatten<T>β element type whenTis an array, otherwiseTMyNonNullable<T>β removesnullandundefined
- Implement a runtime helper
process(value)that:- Returns the first element when
valueis an array - Returns
valueunchanged otherwise - Uses
Flattenfor its return type
- Returns the first element when
- Export
process
Completion checks:
process([1, 2, 3])β1process(42)β42process(['a', 'b', 'c'])β'a'process(100)β100


