9

The current syntax highlighting for is lang-js. This is presumably due to the fact that lang-typescript was not available in the mod dropdown menu of syntax highlighting options, however, it should now be available for selection.

A simple example of the difference between JavaScript highlighting:

interface Account {
  username: string;
  id: number;
}

vs TypeScript highlighting:

interface Account {
  username: string;
  id: number;
}

This change would also resolve How to have TypeScript syntax highlighting as using the tag name would correctly highlight typescript.

3
  • 4
    While it would be nice to do this, brief testing indicates the problems described in the answer below definitely affect questions. I briefly tried assigning lang-typescript as the default for typscript. The posts I check with both typescript and javascript started using Go syntax highlighting, which seemed substantially more disruptive than using lang-js, which is at least closer. It's a sad statement that the syntax highlighting is still so poorly handled 2 years after the switch to highlight.js. Hopefully, SE will actually fix things.
    – Makyen Mod
    Commented May 25, 2022 at 6:31
  • What does "mod dropdown" refer to? The new editor? What is "mod"? "Modification"? "Moderator"? Commented May 25, 2022 at 8:51
  • 1
    It refers to the default syntax highlighting selector, visible on the tag info page for moderators, @Peter. Henry links to rene's MSE bug report in the same sentence to provide context. There's a screenshot of it at the top of that question.
    – Cody Gray Mod
    Commented May 25, 2022 at 11:23

1 Answer 1

9

This is presumably due to the fact that lang-typescript was not available in the mod dropdown menu of syntax highlighting options

Unfortunately, this is only a partial piece of the puzzle here. Due to limitations of the current highlighting implementation on Stack Overflow (read as: one that desperately needs improvement), reality here is more complicated than it first appears.

I don't think changing the highlighter would necessarily be a bad change to (finally) make– just that there's more to bring up here first.

Primer on Tag Highlighting

As a primer for the unfamiliar, general syntax highlighting on Stack Overflow is driven largely through tag usage on questions. Tags can have a specific highlighter specified to match their topic's language.
E.g. for :

The language highlighter for the JavaScript tag

Having a language defined means that questions tagged with will automatically have code blocks in their body set to use the lang-js highlighter.

The problem, however, arises when a question is tagged with multiple tags with non-matching highlighter definitions– for example, and (the latter is set to lang-html). Stack Overflow's current solution to resolve such conflicts is simply to throw it all away, and use default, language auto-recognition based on code syntax, instead.

For substantial-enough code samples in the right languages, this works fine enough, but the point stands that the current configuration simply throws away the specified highlighters and starts from scratch.

Regarding and lang-js

If we set to use lang-typescript instead of its current setting (lang-js), then all questions tagged with and another tag set to use lang-js would simply start using default, rather than using lang-typescript as the setting implies.

When you factor in the top 3 tags related to that also have lang-js as their highlighter (, , & repectively), this phenomena would affect ~74k questions at time of writing (~40% of all questions).

In a post over on Meta.SE from September of '20, @animuson explains this situation and then argues that keeping things the way they are is the best option currently:

...
Because the javascript tag would be set to lang-js, setting the typescript tag to lang-typescript would only cause that huge portion of questions [tagged with both] to revert to default highlighting, and not get highlighted as either JavaScript or TypeScript, which is not ideal. If TypeScript highlighting is better for a particular question, you can manually specify it for that code block. But overall, leaving the typescript tag as lang-js is far more beneficial ...

Moving Forward

It's a bit of a toss up. Currently, this highlighter mismatch issue affects at least roughly ~40% of questions, which means swapping the highlighter would leave those posts to fend for themselves with the default highlighter. I don't have a good understanding of what sort of impact this would have.

For the other 60%, it's hard to say how much highlighting would really improve, since the lang-ts highlighter is an adjusted superset of the lang-js one. That certainly isn't to devalue having the correct highlighter for TypeScript code, since it very much does matter in many cases; just worth acknowledging that it also doesn't matter in a lot of cases as well.

I think this really hinges on whether setting the 40% to default would have a negative impact or not. If that won't matter that much, then I'd vote for moving forward with this change. I'm not really sure how to efficiently get an answer to that question, however.

4
  • 7
    The "default" (which would be more precisely described as "random") highlighter would be a pretty significant downgrade for those posts IMO. I'd prefer TS being highlighted as JS for all TS questions (like you said, the difference isn't that much) than to have a big chunk of TS questions receive completely inaccurate highlighting. Still hoping they'll revisit an easy fix to this... Commented May 25, 2022 at 1:02
  • Maybe we should start tagging questions with only those that are relevant. Do every question about typescript needs javascript? Or react?
    – Braiam
    Commented May 25, 2022 at 14:21
  • 1
    @Braiam well we have as much influence on how questions are tagged as we have on the weather. Because it's not really up to us. And reviewing every single question that comes in tagged JS + React + TS + Node.JS (or any sub-combination of these) then correcting the tags is a Sisyphean task.
    – VLAZ
    Commented May 29, 2022 at 16:54
  • @VLAZ since the system suggests some tags, we can manipulate it to at least not suggest them. I know it may not help, but really we need to start trying something.
    – Braiam
    Commented May 30, 2022 at 14:04

You must log in to answer this question.

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