28
How do you feel about TypeScript?
(lemmygrad.ml)
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Follow the wormhole through a path of communities !webdev@programming.dev
What are the additional requirements? Afaik valid JS is valid TS.
Perfectly valid, and extremely commonly used, coding pattern in JavaScript - it's essentially the normal way to do an associative array or hashmap in JavaScript. It's also one of the commonly used ways to (poorly) simulate OOP in JavaScript.
In TypeScript, it fails. You can't treat an object as an arbitrary key/value pair. That's a good thing... but still, it means TypeScript is not a superset of JavaScript.
AFAIK that source code will be accepted by the TypeScript compiler if the file has a
*.js
extension, but that's an ugly workaround and it also means you can't copy/paste code between files. You have to rewrite the code.No, it doesn't fail. It compiles to perfectly valid JS that runs exactly as you'd expect. The type checking itself errors, because you've made an error - but the compilation isn't prevented by this error.
So yes, Typescript is a superset of JavaScript.
Wouldn't it fail in strict mode?
The type checking does, but not the compilation.
That is an important difference. Still lots of people, myself included, classify "compiler printing an error (not a warning)" as failure, even if bizzarly the code still runs somehow.
That's because you're missing the distinction between compiler and type checker. The compiler doesn't check types, it strips them. The type checker only checks types, it doesn't compile. They are often used in conjunction, though increasingly the compilation is done by e.g. esbuild.
But there is nothing "bizarre" about the code running, since literally, TS is a superset of JS.
Doesn't change the fact that you can strip types and get js
I know a cool library you can use to convert
.ts
files in Js! It's calledtsc
!/s
Type annotations. It can be as simple a adding
any
in front of parameters, but there are other edgecases too, and when you have a really big codebase it can be a pain to convert.