DEV Community

Discussion on: Getting Started With TypeScript

 
jonrandy profile image
Jon Randy 🎖️ • Edited

and you can by pass the error(ignore it or type cast it), the compiler will still emit the same code where you type it correctly or not

You mean it will leave the JS unchanged? This is not true either (I believe even the TS docs mention this) - some valid JS will be altered by the TS compiler.

Going back to the superset thing - if you take out 'syntactically', then it is possible to argue that TS is actually a subset of JS (or contains a subset of It). Why? Because it's possible to do things in plain JS that it isn't possible to do in TS (without modifying the JS to please the compiler, or turning off TS error checks - but then the code is compiled back to JS anyway)

Thread Thread
 
tylim88 profile image
Acid Coder • Edited

I mean the properly typed and improperly typed will still produce the same code

the type notation will not affect the final JS code emitted

for example If TS decide to compile a to b, with or without the type annotation, the result will still be b

whether a should be b, whether it makes sense or not, is another story

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

TS will mangle this valid JS code - making it do something different:

function f(a) { return a; };

console.log(f<Function>(f)); 
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
tylim88 profile image
Acid Coder • Edited

Because it's possible to do things in plain JS that it isn't possible to do in TS

this should be counted as deficiency, bug, or todo

TS will mangle this valid JS code - making it do something different:

ya like I said, whether 'a' should compile to 'b' is another story, even babel itself has many way to compile the same JS code, and by this logic should we say JS itself is invalid JS?

It really depend on the understanding of the author, they could be right, they could be wrong

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Yep, the problem is (as I said) - the widespread misunderstanding of what 'TS is a superset of JS' actually means. I've lost count of the number of times I've heard people say that 'all your existing code written in JS will run with no problems in TS, you can just add the types gradually' - and then watching the ensuing chaos.

I've written libraries for JS that some really good TS developers I know have tried to make compatible with TS... they gave up!

TS can certainly be the right tool in some situations, but other situations it actually blunts the power of JS.

Thread Thread
 
tylim88 profile image
Acid Coder

The misunderstanding is people think by copy and paste think will work

No, by superset it means the compiler understand JS syntax

it has nothing to do with the effort of the developer, it is not about the developer!