why we need it?
In truth, you don’t need it. Some people like to use it as an extension of JS.
But you don’t need TS, especially if you’re just doing these FCC projects. You might someday need it, if you work at a company that uses it, have to maintain code written in TS, or decide that you really, really need some of those features that it has.
My guess is, that if you have to ask what it is, you probably don’t need it, not yet anyway. Most JS coders don’t use TS. That’s not to say that there’s anything wrong with it, just that it isn’t needed. And it would probably just confuse matters at this point in your learning.
It’s good to know what it is, but I wouldn’t worry about learning it until you have mastered JS.
You do need typescript if you work with angular 2 or 4.
Perhaps, but since you don’t need Angular (at least not at what I infer is the OP’s level), that still means that you don’t need TypeScript. I’m not saying it isn’t useful, just that for what we’re doing on FCC, it isn’t needed and may be an unnecessary distraction.
Well you wrote that most js coders don’t use typescript when in fact a lot of them do.
So he might never need it since React is where’s it at.
I know people have their pet technologies. I know tech people don’t to admit that they were wrong. But by no stretch of the imagination do most coders use TypeScript, not even close. That’s just math. You can have opinions about what you like and don’t like, but numbers don’t care about feelings.
Again, I’m not saying there’s anything wrong with TS, just that (to the OP’s concerns) he does not need to learn TS, especially if following the FCC curriculum. Maybe it will be useful in the future, but if he has to ask what it is, he doesn’t need it for now.
the primary difference between js and ts is suggested by its name “typescript” - ts adds static type checking to js - js has a few types - currently 7 - but its type system is dynamic
dynamic types in js means you cannot declare the type of a variable - the type is determined at runtime - not only that but the same variable can be used for values of different types
this is fine in js because
x is only initialized as a string - it can be assigned a number later
let x="Ali" x=21
this sample file
we can run
tstest.ts with node.js and see the output
node tstest.ts I am Ali, 21 years old
tsc - you have to install the typescript compiler and run this
tsc --pretty --listEmittedFiles tstest.ts
which shows the converted js file
then you can run or use the generated
tstest.js file like any js file - and get the same output as running
node tstest.js I am Ali, 21 years old
let’s add a line to
node tstest.ts I am Ali, 21 years old I am 21, Ali years old
we see dynamic typing in action - js has no problems using a number for a string or vice versa even though the second line of output is likely unintended
tstest.ts to use ts types
we’ll use the
tsc --strict option to enforce typescript types
tsc --pretty --listEmittedFiles --strict tstest.ts TSFILE: tstest.js node tstest.js I am Ali, 21 years old I am 21, Ali years old
the generated js ran fine again because the
any type in ts allows dynamic typing to work even with strict static type checking
tstest.ts to use more specific types
tsc --pretty --listEmittedFiles --strict --noEmitOnError tstest.ts 7 console.log(iam(21, "Ali")) ~~ tstest.ts(10,17): error TS2345: Argument of type '21' is not assignable to parameter of type 'string'.
this time typescript caught the swapped parameters passed to
iam - we don’t have to run the program to discover the wrong values used by the function