What is TypeScript?

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

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 Likes

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.

3 Likes

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

1 Like

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.

1 Like

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

“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.

1 Like

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

@prohorova way to overcomplicate things! I found this answer by searching “what is typescript?” in google. The first answer was concise but sufficient to clarify the question I had.
To start an argumentation on how many programmers (in percentage or perceivably), or what is “a lot”, “most” and “many” doesn’t add any beneficial information imho.

Was it necessary to make such a hostile reply on a two year old topic?

TypeScript is one of the fastest growing languages, and in my opinion, it is the future of front end development. I use it every day, and it has completely transformed the way I code. It’s also incredibly popular amongst its users, as the Stack Overflow survey shows.

Kevin Smith is right to say that you don’t need it for FCC. But it is well worth learning because static typing is a core concept of practical programming, and because it will help you understand JavaScript better and write better code. Also, if you ever need to work with Java or C#, knowing TypeScript will be a huge benefit.

Typescript was designed to help java developers migrate easily to a javascript world !

Typescript brings java concepts into javascript and therefore it is easier to learn for someone who is coming from a java programming background !

Hope that helps !

Typescript makes you better at javascript. Better at react. Better at node. Just learn it.

1 Like