15 ms·
A complete guide to TypeScript’s 'never' type
- throwra620 5y ago> Since there’s no values in the set, never type can’t never (pun-intended) have any value That’s not a pun that’s just poor grammar. “never type can never (pun-intended) have any value” makes sense.
- blackoil 5y agoI have seen such double negative at many places, to represent strong negative. Don't know if it is some trend!
- nosianu 5y agoFunnily enough, in Russian the double negative is completely normal and that is a normal way to express many negatives. It always triggers my brain when I have to say a double negative in Russian, "just do it, it's completely normal, don't worry about it..." (inner feeling-based monologue). https://www.russiantutoring.com/post/double-negation-in-russian-when-one-person-s-no-is-another-s-yes https://www.russiantutoring.com/post/double-negation-in-russ...
- Latty 5y agoReminds me of a classic linguistics joke (I assume from a Tom Scott video given where I encounter linguistics the most): A professor is lecturing on linguistics: "In English a double negative has a positive meaning. However, in some languages—such as Russian—a double negative still has a negative meaning. There isn't, however, a language in which a double positive holds a negative meaning." A student in the back responds: "Yeah, right."
- ratzkewatzke 5y agoThis is widely attributed to Sidney Morgenbesser (https://en.wikipedia.org/wiki/Sidney_Morgenbesser https://en.wikipedia.org/wiki/Sidney_Morgenbesser).
- eyelidlessness 5y agoThis reminds me of ruminations with a friend about the nuance of combined positive/negative affirmations. Eg “no yeah” used as an emphatic agreement in the positive, and “yeah no” an emphatic agreement in the negative.
- mananaysiempre 5y agoThe high-brow name for this syntactic gadget is “negative concord”[1], and Wikipedia lists plenty of languages that have it[2] (including several European ones and even Old English!). [1] http://glottopedia.org/index.php/Negative_concord http://glottopedia.org/index.php/Negative_concord [2] https://en.wikipedia.org/wiki/Double_negative https://en.wikipedia.org/wiki/Double_negative
- eyelidlessness 5y agoIt’s fairly common usage in the US southeast, and in AAVE. Regarding it as poor grammar rather than an evolution of dialect is a matter of (snobbish) taste more than anything.
- jsmith45 5y agoOne major problem with it is that even in those dialects, a double negative can also be a positive, and outside of some semi-fixed constructs that mandate one or the other interpretation in that dialect, the distinction relies things like tone and/or stress/emphasis, which of course are lost in writing. This makes it rather unwise to use in writing, unless context clearly dictates one interpretation.
- anderskaseorg 5y agoThe more northern dialects are infected with certain negation-related illogicalities too. Consider this New York Times headline: “All Options Are Not on the Table as Biden Moves Troops Closer to Ukraine” https://www.nytimes.com/2022/02/05/us/politics/biden-ukraine-russia-war.html https://www.nytimes.com/2022/02/05/us/politics/biden-ukraine... which should have been “Not All Options Are on the Table”, since the intended interpretation is ¬∀ option, on(option, table) rather than the very different ∀ option, ¬on(option, table).
- eyelidlessness 5y agoI am never not confused by this.
- DonaldPShimoda 5y ago> That’s not a pun that’s just poor grammar. There is no such thing as "poor grammar" --- it's a myth. Most of the grammatical "rules" we tend to be taught in primary and secondary school English courses were invented relatively recently, and they were created specifically for the purpose of establishing an arbitrary class distinction. Now, there's something to be said about markedness [1]. If the majority of a specific group of speakers of a language find a particular utterance (phrase, sentence, pronunciation, etc) to stand out in a way that feels unnatural, then that utterance is called "marked". Clearly, "can't never" is marked in your perspective. (And, indeed, I also find it to be marked in my own dialect.) However... There do exist large groups of native English speakers for whom this utterance is not marked. In these groups, this is a regular way to emphasize a negative. So, in those groups, the utterance is unmarked. So it is wrong to say that your notion of markedness is universal. Undoubtedly, there are utterances you would produce that members of other groups of native English speakers would find marked. (A simple example offhand: American English speakers find the British constructions of "go to hospital" or "to be on holiday" to be marked. Does that make these utterances ungrammatical universally? Absolutely not.) Context is important. If you are writing, say, an academic paper that you want to publish at a particular venue, then you are expected to adhere to the grammatical conventions of that venue. But for general everyday speech? The rule is straightforward: if you understand the intent, then it is not incorrect. It might be marked for you, but that does not make it "wrong" or "bad" or "poor" universally. [1] https://en.wikipedia.org/wiki/Markedness https://en.wikipedia.org/wiki/Markedness
- exdsq 5y agoAny sources on poor grammar being a new thing? It's been taught in public/private schools in England for centuries, maybe even millennia.
- giraffe_lady 5y agoPedagogical history isn't a strong argument for something's truth, just its usefulness to someone. Theology was considered a key component of a higher education for centuries and is still taught for example. People have always strongly believed, and taught, things about their own languages and languages in general. They're not all wrong necessarily. But linguistics over the last like 80 years has produced some models that have incredible predictive and explanatory power, and are incompatible with a view of an objective "correct" or "best" or "standard" form of a language.
- eyelidlessness 5y agoIrregardless of your subjective opinion about grammar, you literally expressed the exact same thing. It’s either a pun in both or it’s a pun in neither.
- gururu 5y agohey thanks for pointing that out. I should have said "can never" instead. it is hard being a non native english speaker
- iconical 5y agonice
- sod 5y agotldr: A method that is typed as returning `never` either never returns (infinite while/for loop) or throws an error. And as return types can also be conditional, your given inputs may switch the return to `never`, allowing typescript to tell you that code following that line will never be executed.
- city41 5y agoThis is only one use case for never. The article goes into a lot more than this. For example using never to create derived types is pretty common.
- brewmarche 5y agohttps://en.wikipedia.org/wiki/Bottom_type#In_programming_languages https://en.wikipedia.org/wiki/Bottom_type#In_programming_lan... if you want to know what it’s called in other languages (for some reason it doesn’t mention `Void` for Haskell though)
- _greim_ 5y agoWhat programmer hasn't at some point in their career written code like this: throw new Error(`Unexpected value: ${value}. This should never happen!`); ...and then had a user submit a bug report containing that very message. The `never` type is a nice way to model those scenarios and hopefully avoid such an error ever being seen by the user. // Compile error unless we've ruled out // `value` at the type level assertNever(value, new Error(`Unexpected value: ${value}.`));
- edflsafoiewq 5y agoThe first one is a very useful error as a user though. It tells me the unexpected value isn't my fault and I should go report a bug.
- chii 5y agoit's not a useful message to the laymen. The message is a contradiction, which makes no sense to an untechnical person (who is also unlikely to report it properly, from experience). The will say that the computer is broken, or that the program stopped. Ideally, the user is presented with a general message telling them how to report a bug (link or something?), and include a base64 encoded blob, containing the context of the error (e.g., a stack trace, and may be also the very message in the exception), and ask the user to also submit that blob.
- edflsafoiewq 5y agoWow, I would hate that. Fortunately I've never seen it before. Seems like "the user is too dumb to lay their lowly eyes upon our error message".
- cercatrova 5y agoThat is correct, the average user does not know and does not care to know about the inner machinations of the software. They aren't "dumb," they just don't care. And so the onus should not be on them to file your bugs.
- vasergen 5y agoDidn't know it is possible to do exhaustive matching in TypeScript, quite impressed to be honest, but this looks a bit too complicated to be used widely
- joshuakgoldberg 5y agoGreat writeup on `never`, love to see it!
- DonHopkins 5y agoAs long as you're stuck with null and undefined, you might as well throw in never for $3 billion! "My favorite is always the billion dollar mistake of having null in the language. And since JavaScript has both null and undefined, it's the two billion dollar mistake." -Anders Hejlsberg "It is by far the most problematic part of language design. And it's a single value that -- ha ha ha ha -- that if only that wasn't there, imagine all the problems we wouldn't have, right? If type systems were designed that way. And some type systems are, and some type systems are getting there, but boy, trying to retrofit that on top of a type system that has null in the first place is quite an undertaking." -Anders Hejlsberg
- hajile 5y agoBut it's worse that that. var foo = new Array(5) That code will make an array with 5 elements, but their values aren't actually null or undefined, but another kind of "empty" value. You can't access this value directly (it will convert to undefined), but it still exists. EDIT: there's another kind of undefined that happens within the Temporal Dead Zone too
- nybble41 5y agoThis `never` type is the opposite of a null value, though. The "billion dollar mistake" (which is really the absence of non-nullable references, not merely the presence of null or undefined) adds a possible value to every reference. At any point you could have a valid reference, or a null reference, and this makes it impossible to express the type of a reference which must be valid. The `never` type by contrast has no possible values. It offers a way to statically assert that nothing has been left unhandled. A type system without `never` (or equivalent) is like a number system without zero. (Literally—`never` is the zero for algebraic data types.)
- vulneraball 5y agoIs the never type just an alias for an empty union, or are these two somehow different types in TypeScript ?
- iron4wine 5y agoThis is the missing docs on the type never. Very nice write-up!
- christopherbalz 5y agoSweet! 'never' seems to have historical roots in IE's ancient 'unknown' js type.