Understanding the difference between Typescript’s void and never

const thisThrows = (): never => {
throw('someError');
return 'thisNeverHappens';
}
const thisDoesNotReturnAValue = (): void => {
someGlobal = 1 + 2;
}
const returnValue = getAStringOrVoid();returnValue.toUpperCase(); // this is unsafeif (typeof returnValue === 'string') {
returnValue.toUpperCase(); // this is safe
}
const anotherReturnValue = getAStringOrThrow();
anotherReturnValue.toUpperCase(); // this is safe

--

--

--

Javascript Developer — Manchester

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas James Byers

Thomas James Byers

Javascript Developer — Manchester

More from Medium

Don’t Develop Drunk

What are REFs in React JS and Where To Use Them

One time array loop