MediumConditional Types💻 Output Question

NonNullable conditional — null and undefined are removed

💡

Hint

NonNullable<T> is implemented as T extends null | undefined ? never : T. When used on a union like string | null, the conditional distributes: string | (null extends null|undefined ? never : null) = string | never = string.

What does this output?

// Simulate NonNullable<T> = T extends null | undefined ? never : T
// At runtime, demonstrate which values would survive NonNullable

function nonNullable(val) {
  if (val === null || val === undefined) {
    return 'never';  // TypeScript: resolves to never
  }
  return typeof val; // TypeScript: resolves to T (non-nullable version)
}

// TypeScript compile-time resolution:
// NonNullable<string | null>       => string
// NonNullable<number | undefined>  => number
// NonNullable<null | undefined>    => never
// NonNullable<string>              => string

console.log(nonNullable('hello'));
console.log(nonNullable(42));
console.log(nonNullable(null));
console.log(nonNullable(undefined));
console.log(nonNullable(false));

Correct Output

string
number
never
never
boolean

Why this output?

Explanation: NonNullable removes null and undefined from a type, resolving to never for pure null/undefined types. All other types pass through unchanged.

Key Insight: NonNullable is implemented as T extends null | undefined ? never : T. When used on a union like string | null, the conditional distributes: string | (null extends null|undefined ? never : null) = string | never = string.

More Conditional Types Output Questions

MediumIsArray conditional — resolves based on type checkHardinfer extracts the wrapped type from a generic containerHardDistributive conditional — union members resolved independently

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz