[HN Gopher] A complete guide to TypeScript's 'never' type
___________________________________________________________________
A complete guide to TypeScript's 'never' type
Author : bpierre
Score : 80 points
Date : 2022-03-09 17:03 UTC (2 days ago)
(HTM) web link (www.zhenghao.io)
(TXT) w3m dump (www.zhenghao.io)
| vasergen wrote:
| Didn'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
| sod wrote:
| tldr: 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 wrote:
| This 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.
| iron4wine wrote:
| This is the missing docs on the type never. Very nice write-up!
| _greim_ wrote:
| What 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 wrote:
| The 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.
| DonHopkins wrote:
| As 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 wrote:
| But 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 wrote:
| This `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.)
| brewmarche wrote:
| 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)
| iconical wrote:
| nice
| throwra620 wrote:
| > 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.
| eyelidlessness wrote:
| Irregardless 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.
| blackoil wrote:
| I have seen such double negative at many places, to represent
| strong negative. Don't know if it is some trend!
| nosianu wrote:
| Funnily 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-
| russ...
| mananaysiempre wrote:
| The 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
|
| [2] https://en.wikipedia.org/wiki/Double_negative
| Latty wrote:
| Reminds 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 wrote:
| This is widely attributed to Sidney Morgenbesser
| (https://en.wikipedia.org/wiki/Sidney_Morgenbesser).
| eyelidlessness wrote:
| It'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 wrote:
| One 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 wrote:
| The 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...
|
| 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 wrote:
| I am never not confused by this.
| DonaldPShimoda wrote:
| > 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
| robocat wrote:
| And beware that markedness is pronounced as three syllables
| like mar*ked*ness[1]
|
| Is marked pronounced as two syllables, mar*ked, in the
| context of linguistics? I am guessing it would definitely be
| in British English[2].
|
| [1] https://m.youtube.com/watch?v=vrZpjr44c1w
|
| [2] https://www.reddit.com/r/linguistics/comments/dwo5gj/is_i
| t_a...
|
| Sorry for the poor source links, I only did a quick Google.
| Disclaimer: I am from New Zealand, and sometimes how we say
| things is special, and sometimes how I say things is even
| specialer.
| DonaldPShimoda wrote:
| In my linguistics courses (at a university in the United
| States), professors pronounced "marked" as a single
| syllable.
|
| Generally, when indicating we want to give an E that is
| normally silent its own syllable, we use the grave accent.
| E.g., for "marked" we would write "marked".
|
| At least in General American dialects, "marked" is rendered
| as /markt/ because of rules about vowel epenthesis and
| consonant voicing assimilation. /markd/ would indeed sound
| odd to the American ear, I think. But when you explicitly
| add the vowel back in, the voiceless /k/ no longer forces
| the /d/ to assimilate its voicing characteristic, so you
| get /'mar.ked/. (I am using informal IPA because I'm too
| lazy to go copy/paste the specific symbols, but in this
| context they would add little anyway.)
| tshaddox wrote:
| I mean, I agree with all that, but also in practice there
| actually are English utterances that a vast majority of
| native English speakers will interpret as indicative of a
| profound lack of familiarity with the English language, and
| it's reasonable to describe those utterances in normal
| conversation as being "grammatically incorrect."
| DonaldPShimoda wrote:
| The technical term for such utterances is "ungrammatical",
| for what it's worth. This is a term with less uhhh shall we
| say "ideological baggage" than one built with the word
| "incorrect".
|
| That said, the specific example being cited from the
| original blog post is _not_ considered marked in many
| native English dialects.
|
| And, indeed, I believe that we should be less concerned
| with such labels even with non-native English speakers.
| Since English has effectively become the world's go-to
| language of international communication, we should seek to
| understand other people's intent rather than attempt to
| correct arbitrary (and, frankly, unimportant) artifacts in
| their grammatical constructions.
| tshaddox wrote:
| > The technical term for such utterances is
| "ungrammatical", for what it's worth. This is a term with
| less uhhh shall we say "ideological baggage" than one
| built with the word "incorrect".
|
| But my point is that people will use phrases like
| "grammatically incorrect" in casual conversation (far
| from any context of linguistics or any social science) to
| describe certain utterances, and there are cases where
| this is _not_ unreasonable.
| bckr wrote:
| I'm sure there's some validity to this from an
| anthropological/linguistic standpoint (I'm a big fan of
| "sounds prescriptivist but ok").
|
| But look at your own comment:
|
| > 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.
|
| This is a technical blog post addressing a technical
| audience. In fact the sentence in question is explaining a
| technical concept. Therefore the (unwritten?) rules of
| technical writing apply.
|
| So it's inappropriate here to give lection on linguistic
| relativism.
|
| For the purposes of the article, its audience, and the
| particular sentence in question, this is poor grammar.
| DonaldPShimoda wrote:
| > This is a technical blog post addressing a technical
| audience.
|
| I disagree; it is a _personal_ writing. If I write a blog
| post, I can use whatever grammatical constructions I like.
| I don 't think there should be any expectation of a
| particular grammatical style in an individual's personal
| writings.
|
| This was not a peer-reviewed article, nor was it submitted
| for publication at a specific venue. It was self-published
| on the internet.
| ghusbands wrote:
| Given that the OP has posted to say that it was a mistake and
| hence isn't "unmarked" as you put it, you've quite missed the
| point. Not all mistakes (and corrections) are evidence of
| some classist plot.
| exdsq wrote:
| Any sources on poor grammar being a new thing? It's been
| taught in public/private schools in England for centuries,
| maybe even millennia.
| DonaldPShimoda wrote:
| Sorry, I think I was not clear enough.
|
| There have certainly been efforts to draw arbitrary
| distinctions between groups based on speech for a very long
| time, probably going almost as far back as language itself.
|
| What I was referring to was specifically an effort rooted
| in a desire to use rules of Latin grammar to restrict
| constructions in English. This was a big thing relatively
| recently (likely sometime in the past 100-300 years). It is
| this movement from which we get rules like "never end a
| sentence with a proposition" or "don't split infinitives"
| --- those sorts of things.
| ghusbands wrote:
| Those things are irrelevant to the question of whether
| "never can't never" is incorrect in context, which is
| what you were replying to. Nor was the comment you
| replied to trying to draw distinctions between groups.
| giraffe_lady wrote:
| Pedagogical 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.
| exdsq wrote:
| I think it is relevant though if the original point was
| that grammar is a new thing to solidify class structures.
| I'm not saying that class structures and correct grammar
| aren't relevant, I've been corrected by drunk humanity-
| studying Etonians enough times at the pub, but I don't
| believe that it's some recent invention to solidify class
| structures.
| VyperCard wrote:
| But what do they say about poor?
| LudwigNagasena wrote:
| >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.
|
| That's a very hefty accusation. Do you have any proof that
| "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".
|
| Also known as "poor grammar".
|
| >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.
|
| Yep, in those groups the utterance isn't considered poor
| grammar.
| DonaldPShimoda wrote:
| > Do you have any proof that "they were created
| specifically for the purpose of establishing an arbitrary
| class distinction"?
|
| Apologies; you are correct that my assertion is overly
| strong. I should rather have said "although we can no
| longer ascertain for certain the purpose for which these
| rules were introduced, some believe the origin to be rooted
| in a desire for creating an arbitrary class distinction."
|
| Many of the "rules" we were taught can be traced directly
| back to an ongoing movement in the relatively recent past
| (like within the last 100-300 years, I think) to regularize
| English according to Latin grammar. This includes rules
| like:
|
| - never end a sentence with a preposition
|
| - never begin a sentence with a conjunction
|
| - never split an infinitive
|
| But we can find literary examples breaking any of these
| rules going back further than the origins of the rules. The
| rules were made up by people who revered Latin and thought
| English ought to be more Latin-esque.
|
| The correlation to class distinction comes from the fact
| that people who tended to study Latin and revered Latin in
| this manner were members of an aristocracy, theocracy, or
| similar upper class, and there are other instances of these
| groups' efforts to arbitrarily distinguish themselves as
| being distinct from and superior to other groups of people.
|
| There are also more recent efforts in a similar vein, as
| well. For example, in the United States there is a dialect
| commonly practiced by Black people in certain areas called
| African American Vernacular English. This dialect has a
| number of characteristics that are frequently labeled as
| "uneducated" or constituting "poor grammar". Although it is
| not controversial to say that these characteristics are
| marked in General American dialects, the specific
| connotations of the labels are generally understood to be
| rooted in a desire to portray Black Americans as belonging
| to a lower class, regardless of each individual's actual
| socioeconomic status.
|
| > Also known as "poor grammar".
|
| No, markedness is _not_ also known as "poor grammar".
| There is a huge distinction.
|
| The notion of markedness is that, for some group of people,
| a given utterance will strike them as not being something
| they themselves would say. Markedness is always framed in
| terms of a cultural context, by which I mean that it is
| okay to say that Americans generally find the British "go
| on holiday" to be marked --- it sticks out in the American
| dialects. This is a subjective assessment.
|
| But "poor grammar" specifically seeks to attribute a
| negative connotation to the utterance. It says suggest that
| people who construct such utterances are, somehow,
| _uneducated_ , or else are _outsiders_. It says that they
| 've done something that is _objectively_ (not subjectively)
| incorrect or wrong.
|
| > Yep, in those groups the utterance isn't considered poor
| grammar.
|
| Not always. Again, the terms are distinct in intent.
|
| I don't think most Americans would listen to an English
| person say "I will go on holiday" and call it "poor
| grammar". They might say "that sounds funny" or "I'd never
| say it that way" or whatever else, but I think most would
| not use a phrase with such a strongly negative connotation.
| Many Americans view people with English accents
| (specifically accents closer to Received Pronunciation,
| though others are included) as having increased
| intelligence or other intellectual merit, for whatever
| reason, and so they will avoid labeling the speech of such
| people to be "incorrect" or "wrong" or "bad"; they will
| instead simply label it as "different".
|
| However, when many (especially white) Americans hear
| utterances like "I been going to the store" or "I done
| finished my homework" or "I ain't never seen such a thing",
| they will label those as "poor grammar". They are
| specifically making a negative assessment of such
| utterances.
|
| But all the examples are considered equally marked in
| General American dialects.
|
| So my desire to distinguish markedness from "poor grammar"
| is rooted in a desire to fight stigmatization. There is
| nothing inherently _wrong_ with those latter examples I
| gave, especially because almost all native English speakers
| will understand their intents (at least generally) without
| issue.
| LudwigNagasena wrote:
| >I should rather have said "although we can no longer
| ascertain for certain the purpose for which these rules
| were introduced, some believe the origin to be rooted in
| a desire for creating an arbitrary class distinction."
|
| That sounds more believable. Some people believe that,
| and some people believe that moon landing wasn't real.
| Many languages like Spanish, French, Russian or Chinese
| are regulated on the national or even supranational
| level. That feels like a good thing and it shouldn't be
| discouraged just because some people believe in weird
| things.
|
| >No, markedness is not also known as "poor grammar".
| There is a huge distinction. >But "poor grammar"
| specifically seeks to attribute a negative connotation to
| the utterance. It says suggest that people who construct
| such utterances are, somehow, uneducated, or else are
| outsiders. It says that they've done something that is
| objectively (not subjectively) incorrect or wrong.
|
| Markedness is just as objective as poor grammar. It is
| all about a specific group of people in a specific time
| and place. As time changes so does grammar and what is
| consider poor.
|
| >Not always. Again, the terms are distinct in intent.
|
| Well, in the strict sense "markedness" has different
| meaning. The way you described more or less resembles
| poor grammar in most cases.
|
| >I don't think most Americans would listen to an English
| person say "I will go on holiday" and call it "poor
| grammar". They might say "that sounds funny" or "I'd
| never say it that way" or whatever else, but I think most
| would not use a phrase with such a strongly negative
| connotation.
|
| They probably understand that it is a standardized
| learned prestige dialect.
|
| >So my desire to distinguish markedness from "poor
| grammar" is rooted in a desire to fight stigmatization.
| There is nothing inherently wrong with those latter
| examples I gave, especially because almost all native
| English speakers will understand their intents (at least
| generally) without issue.
|
| Yes, I understand. Americans have a knee-jerk reaction
| against everything that feels a tiny bit racist. However
| propping up a single standardized language and
| discouragement of deviations is a path that most nation-
| states have embraced.
| ghusbands wrote:
| > But "poor grammar" specifically seeks to attribute a
| negative connotation to the utterance. It says suggest
| that people who construct such utterances are, somehow,
| uneducated, or else are outsiders. It says that they've
| done something that is objectively (not subjectively)
| incorrect or wrong.
|
| You're completely ignoring that people do make mistakes
| and get corrected. There is nothing in the post you
| originally replied to implying that anyone is uneducated
| or an outsider - you're introducing that. "Poor grammar",
| "bad grammar" and "incorrect grammar" are interchangeable
| and aren't about class.
|
| If I accidentally type "Eat I bread", it's incorrect
| grammar even if there happens to be some dialect
| somewhere where it is valid.
| jfengel wrote:
| I see "poor grammar" in the context of people writing things
| that they would never say, and would mark as wrong if they
| were to read it out loud.
|
| I also see "poor grammar" from non-native speakers. Including
| me: Duolingo was correct today when it told be that I
| shouldn't use "tu es" as a French subjunctive. No native
| speaker would say that. I just couldn't hear that what I
| typed was different from "tu aies" -- though I should have
| known from context.
|
| You're absolutely right that much of what people tag as "poor
| grammar" is in fact a different dialect. It's perfectly
| natural to them and the people around them, following a set
| of rules that they know but may never have written down. They
| may evolve from mistakes that become persistent and thus
| valid in context.
|
| Speaking in the wrong register or dialect is an error of its
| own -- not a grammatical one, but a semiotic one.
| robocat wrote:
| FYI: Neither 'tu es' nor 'tu aies' make any sense to me
| (disclaimer: I have only learnt Spanish on the street).
| https://www.spanishdict.com/conjugate/ser
|
| Perhaps you were trying to write down what you heard? 99%
| of the time there is a single canonical translation from
| verbal to written and vice-versa; English speakers have
| trouble learning that for some reason.
| jfengel wrote:
| Sorry, I should have provided context. This was French,
| not Spanish. (I've clarified it in the original comment.)
|
| If it had been Spanish, transcribing it would be easier.
| French mooshes a lot of things together, so that "es" and
| "aies" are both pronounced "eh".
| hota_mazi wrote:
| Note that they are two different verbs (to be and to
| have, respectively), so the context should have made it
| clear which one you should use.
| jfengel wrote:
| Yep. It was completely clear. I just wasn't thinking
| clearly.
| robocat wrote:
| I think it is generally polite when editing to add
| something to show you edited it. Especially when you
| significantly change details, or if the change is due to
| a comment, otherwise the thread looks like nonsense.
| gururu wrote:
| hey thanks for pointing that out. I should have said "can
| never" instead. it is hard being a non native english speaker
| joshuakgoldberg wrote:
| Great writeup on `never`, love to see it!
| christopherbalz wrote:
| Sweet! 'never' seems to have historical roots in IE's ancient
| 'unknown' js type.
___________________________________________________________________
(page generated 2022-03-11 23:01 UTC)