Skip to content

Instantly share code, notes, and snippets.

@danopia
Created January 12, 2021 21:24
Show Gist options
  • Save danopia/07e3c6574e62adb6a4f83260cf43355c to your computer and use it in GitHub Desktop.
Save danopia/07e3c6574e62adb6a4f83260cf43355c to your computer and use it in GitHub Desktop.
typescript inconsistent never behavior
// variable we want to narrow
const { channel } = {} as Record<string,string | undefined>;
// if this simpler definition is used instead, it's all ok
// const channel: undefined | string = 'hi';
// method returning never
const api = {} as { cancel(): never; };
if (!channel) api.cancel();
channel.length; // error... possibly undefined
// function returning never
const cancelType = (() => {}) as () => never;
if (!channel) cancelType();
channel.length; // error... possibly undefined
// implemented function returning never
function cancelReal(): never { while (true) {} }
if (!channel) cancelReal();
channel.length; // this is ok??
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment