23

I frequently see a question in the TypeScript tag where an asker does some logic that semantically should be enough to check that a given type supports a certain operation, but does not satisfy the compiler, and the answer to the question is that they need to use one of the methods the compiler understands (typeof, instanceof, user-defined type guard) explained in the TypeScript handbook.

Glancing through my TypeScript answers, at least half of them are variants on "use a custom type guard" link to playground, link to the documentation.

But strictly speaking, they arguably are not duplicate questions, even though the answer is pretty much the same in every case.

Is there a canonical question for this? Is a canonical even desirable in this case?

15
  • 10
    "Is a canonical even desirable in this case?" yes, please. I've been leaving a lot of comments that point to the documentation. It's something that should exist as a canonical on the site here.
    – VLAZ
    Feb 3, 2021 at 12:20
  • Sounds like they are duplicate questions, of "a question where an asker does some logic that semantically should be enough to check that a given type supports a certain operation but does not satisfy the compiler". Feb 3, 2021 at 14:20
  • 2
    In fact the documentation is not even complete (or am I missing somethind?): { type: "test" } | { type: "other } can also be typeguarded with .type === "test". And I think there are some more cases, so yeah, having a Q&A to collect all of them sounds like a great idea. Feb 3, 2021 at 23:08
  • 3
    @JonasWilms that's a discriminated union which is covered in another part of the documentation. And yes, I agree that it'd be useful to have that information alongside type guards. Since the official documentation doesn't do it, perhaps some public repository of knowledge might be a fitting place.
    – VLAZ
    Feb 4, 2021 at 7:27
  • @VLAZ what would a Q&A offer other than regurgitating the documentation?
    – Braiam
    Feb 4, 2021 at 10:50
  • 2
    @Braiam For better or worse the ability to have a future-proof link-only answer (=mark as duplicate). Link-only answers are bad because the links might expire. Link-only answer to another answer is bad because there is a dedicated solution for that: duplicates. Feb 4, 2021 at 11:24
  • @DavidMulder in other words, repeating the documentation. There will be more duplication of content that way. We really should start pushing people to read the documentation first or improve the documentation so they can find the relevant articles.
    – Braiam
    Feb 4, 2021 at 12:09
  • 5
    @Braiam RTFM put politely or impolitely is not welcome on SE. The goal is to answer all questions here on SO, which obviously means that there will be a lot of overlap with documentation. SO Documentation was a project which wanted to 'solve' this in a different way, but after that was closed we're now back to the status quo. If anything SO trifes of and rewards RTFM questions, as they support the "google a question -> find the same question -> find an answer" Feb 4, 2021 at 12:30
  • 2
    @DavidMulder "The goal is to answer all questions here on SO"? No, nonono. The goal is to create a repository of high quality information. If we don't have anything to offer over existing content, I doubt it's high quality. Not all questions should be asked on SO after all.
    – Braiam
    Feb 4, 2021 at 12:44
  • 3
    @Braiam meta.stackoverflow.com/questions/253473/… and just look at the top question on SO (e.g. 4th highest is asking what the "yield" keyword means in python). Reiterating, there is a reason RTFM and LMGTFY are banned on SO. And I think you copied the wrong link, the one you added is about a question being too broad/opinion based for SO. Feb 4, 2021 at 12:49
  • @DavidMulder again, a repository just regurgitating existing documentation is not sustainable. If we don't have anything of value to add, we may as well do nothing.
    – Braiam
    Feb 4, 2021 at 13:33
  • 4
    @Braiam that is certainly true in the general case. But what prompted this question is that that specific question gets asked over and over again. I personally am ok with "regurgitating existing documentation" once and closing as dupes rather than endless ad hoc answers, and as the debate here shows closing them for other reasons is an endless argument. We don't have to do it with every possible question, just the ones that crop up over and over. Feb 4, 2021 at 13:37
  • The problem with ""regurgitating existing documentation" once" is that programmers like consistency, and will claim that because we did it once, we must do it every time. Just see the latest year in moderation. Locking should be the last option, yet it grew in a ~300%, while post deleted just grew a 9%.
    – Braiam
    Feb 4, 2021 at 15:32
  • Documentation is great but it is not very often written in a goal-oriented manner. As an example I have to deal a lot with Spring Security and the documentation is... War and Peace. Stack Overflow or a blog post tends to help me in a fraction of the time it would take me to try and extract the same knowledge from the official docs. So to me there is high value in not copying documentation, but restructuring specific bits in a goal-oriented way. How to setup an OAUTH2 resource server? Well let me tell you!
    – Gimby
    Feb 11, 2021 at 15:18
  • Ok I made one stackoverflow.com/questions/66233214/… Feb 16, 2021 at 22:44

1 Answer 1

2

Since most seemed to be in favor, I went ahead and posted one:

Why can Typescript not figure out the type in my code?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .