What is TypeScript?

What is TypeScript?
0

#1

What is TypeScript ? Does it related to javascript?
why we need it?


#2

TypeScript is a language created by Microsoft which adds some nice features which JavaScript is missing, the most obvious of which is static typing as is found in many other languages. This allows declaring data types for your variables, which makes code easier to reason about and maintain in larger applications. TypeScript must be compiled to plain JavaScript before it can be run.


#3

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.


#4

You do need typescript if you work with angular 2 or 4.


#5

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.


#6

Well you wrote that most js coders don’t use typescript when in fact a lot of them do.


#7

So he might never need it since React is where’s it at.


#8

“Most” would mean more than half. (There is some ambiguity as to whether it refers to “majority” or “plurality”, but since there are only two possibilities, they are the same in this case.) I don’t think more than half use TS. In this 2017 Stack Overflow survey, for “Most Popular Technologies”, among professional developers, JavaScript came in first with 66.7% and TypeScript came in 9th with 11.3%. Angular is listed separately under “Frameworks, Libraries, and Other Technologies” and among professional developers, that is in the lead but still with only 28.1% (And that doesn’t separate out the non-TS based Angular 1.)

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.


#9

typescript is a language based on javascript - it is supposed to be a superset of js - any js program should be valid typescript

it is open source with an apache 2.0 license - javascript actually ecmascript is also open source with a bsd license - both qualify as free software as defined by the Free Software Foundation - this is very important for people who want to use and support free open source software

https://www.gnu.org/licenses/license-list.html

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 tstest.ts shows the difference between javascript and typescript

// file tstest.ts
// this is javascript - it is also valid typescript
function iam(name, age) {
  return "I am " + name + ", " + age + " years old"
}
console.log(iam("Ali", 21))

we can run tstest.ts with node.js and see the output

node tstest.ts
I am Ali, 21 years old

to use tstest.ts as typescript we have to first convert it to javascript using the typescript compiler tsc - you have to install the typescript compiler and run this

tsc --pretty --listEmittedFiles tstest.ts

which shows the converted js file

TSFILE: tstest.js

then you can run or use the generated tstest.js file like any js file - and get the same output as running tstest.ts

node tstest.js
I am Ali, 21 years old

let’s add a line to tstest.ts

console.log(iam(21, "Ali"))

this is still javascript so we can run it directly as tstest.ts

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

let’s change tstest.ts to use ts types

// this is no longer javascript - it uses typescript types
function iam(name: any, age: any) {
  return "I am " + name + ", " + age + " years old"
}
console.log(iam("Ali", 21))
console.log(iam(21, "Ali"))

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

let’s change tstest.ts to use more specific types

// file tstest.ts
// this is no longer javascript - it uses typescript types
function iam(name: string, age: number) {
  return "I am " + name + ", " + age + " years old"
}
console.log(iam("Ali", 21))
console.log(iam(21, "Ali"))

compile again

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


Can i learn Javascript and TypeScript together?