0

I'm looking at enabling the lint rule @typescript-eslint/no-invalid-void-type. I want to make sure everything it's flagging is truly something I always want to get rid of. Just to check my understanding. Are the following statements correct?

  1. There's never any reason to include void in a type union, like string | void.
  2. There's never any reason to declare a function that takes void as a param. (Except for something like this: void).
  3. There's never any reason to put void in a container generic, like Array<void | string>.
  4. Putting void | undefined on a function return signature (e.g. () => void | undefined) is redundant.
  5. If you have () => SomeType | void, you should use () => SomeType | undefined instead.

And the reason for most of those statements being true is that you can't instantiate a value of type void, so something like Array<string | void> makes no sense. Right?

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • Seems like you're going to get lots of opinion here; what would an authoritative answer look like? – jcalz Jan 21 '22 at 19:18
  • I'm sure you know it, but `this: void` is not "taking a param", it's an ugly (debatable) choice of syntax – Parzh from Ukraine Jan 21 '22 at 19:18
  • I don't think this is opinion-based. For example, "will adding `void` to a type union ever change what the type union evaluates to?" is an objective question. – Nick Heiner Jan 21 '22 at 19:33
  • One caveat to #2 is that [ms/TS#27522](https://github.com/microsoft/TypeScript/pull/27522) implemented so that trailing `void` parameters are considered optional, but this behavior has all kinds of weird quirks. See [this](https://tsplay.dev/N7PBRm). – jcalz Jan 21 '22 at 19:36
  • "will adding void to a type union ever change what the type union evaluates to?" is an objective question I don't see in the plaintext of the question itself. What I see in the question is a set of fairly broad statements, most of which depend on whether there is "any reason" to do something. Could you rephrase to a form which doesn't encourage opinion? Your 4th and 5th statements seem a little less opinion-ish. Does [this](https://tsplay.dev/NrK61m) indicate that statement 4 is false? – jcalz Jan 21 '22 at 19:44
  • Like, if I step back and look at your question while squinting, I'd say that using `void` anywhere except for a return type is often a bad idea, because `void` is wacky and has a few inconsistent uses that play not-so-nicely together. See [this question](https://stackoverflow.com/a/69732504/2887218) and [this question](https://stackoverflow.com/q/65721318/2887218). But that's my opinion. I'm wondering how best to sum this up in a form that isn't just "jcalz's guide to writing TypeScript". – jcalz Jan 21 '22 at 19:50

0 Answers0