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?
Are TypeScript questions, that are not related to types or configuration, just JavaScript questions?
1 Answer
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.
-
1
-
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 -
2Personally 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
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.