Created
January 12, 2021 21:24
-
-
Save danopia/07e3c6574e62adb6a4f83260cf43355c to your computer and use it in GitHub Desktop.
typescript inconsistent never behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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