1

I have seen many questions tagged as TypeScript questions, but in my opinion they are just JavaScript questions written with TypeScript (this one for example, or this one). What should I do in this case? Flag it as duplicate if I find the corresponding JavaScript question?

5
  • 2
    Given that JS is a subset of TS, questions tagged with typescript can indeed be answered by JS answers, so if you find a fitting duplicate, vote! Commented Jun 12, 2020 at 6:54
  • @JonasWilms I think that you are contradicting your own argument: if TS has broader solutions than pure JS does, then dupeclosing would curtail non-JS solutions where they are implicitly desired. Now, if you can claim that if we strip all TS bits and are left with a javascript question, that would be totally different.
    – Braiam
    Commented Jun 12, 2020 at 14:18
  • typescript has no non-js solutions.
    – Kevin B
    Commented Jun 12, 2020 at 14:20
  • @Braiam not "solutions" but "typesafety". arr.sort((a, b) => a - b) would be valid in both JS and TS (although you can tell the compiler to reject it). To turn it into a "real" typescript solution, you'd add types to get typesafety: arr.sort((a: number, b: number) => a - b). Now adding these types is, in most cases, trivial and can be left to the OP. In cases were the typings get more complicated, there can be Typescript questions. Commented Jun 12, 2020 at 14:24

1 Answer 1

1

YES! Questions containing TypeScript can, in a lot of cases, be answered by pure JavaScript answers.

Turning JavaScript into TypeScript can be done by adding types, or just typing anything as any (which TypeScript implicitly does, making JS a valid subset of TS). In most cases, adding typings is trivial, and the actual problem is caused by the way the JavaScript language works. Therefore answers do apply to TypeScript questions, and the OP should be able to adapt the solutions with proper types.

In cases were adding types is not trivial, I'd prefer answering the question with proper TypeScript types, referencing the original answer. Questions asking about types, or TypeScript only syntax (enums, classes), cannot be answered in JavaScript.

4
  • 1
    class is valid JavaScript ;) Commented Jun 12, 2020 at 14:41
  • 1
    @HereticMonkey I certainly know that, but classes in Typescript have a bit more syntactic sugar (e.g. constructor(public prop: type)) Commented Jun 12, 2020 at 14:45
  • 2
    Personally I consider Java Script without enough "type hints" to make it typesafe not to be valid Typescript, as it misses the point of a person using Typescript. Commented Jun 12, 2020 at 20:17
  • @ianRingrose totally agree, however for beginners it might be easier to go with any in more complex cases. Mapped types, conditional types, etc. are not that trivial. Commented Jun 12, 2020 at 20:24

You must log in to answer this question.

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