Skip to content

Empty TypeScript Types

All fields in JS can have the value null or undefined

unless strict mode is enabled

  • unknown
    • I don't know
      • could be anything
    • need to type check/guard
  • any
    • I don't care

never

things that should never happen

const reportError = function (): never {
    throw Error("my error");
};

const loop = function (): never {
    while (true) {}
};
  • prune conditional types
type NonNullable<T> = T extends null | undefined ? never : T;

// NonNullable<MyType> can't be assigned null or undefined
type A = NonNullable<number | null>; // number
  • infinite loops
  • only ever throwing errors

Forcing a number to not be undefined

Only in strict mode???

This still lets myNum be undefined

type NotUndefinedButNullable<T> = T extends undefined ? never : T;
type NumberOrNull = NonNullable<number>;

const myNum1: NumberOrNull = undefined;
const myNum2: NotUndefinedButNullable<number> = undefined;

Last update: 2023-04-24