One morning I woke up and the new technical wave is that I should use typescript instead of javascript.
Okay, wait do you mean I have to start learning a new programming language?
I took a quick peek at a typescript file, boy oh boy this is going to be difficult.
Yeah yeah yeah I know that feeling. So here's a quick guide on how u can understand some basic concepts on switching from javascript to typescript.
Now I'm not here to give you some definition and setup tutorial or why you should use typescript instead kinda lecture. you can find that with just a simple Google search. But trust me on this many developers didn't like the Idea of typescript initially but then when we finally did, we didn't want to go back to plain old javascript and guess what maybe you'll be one too.
let dive in
The whole idea of typescript is being intentional about each step you take in programming.
Declaring variables
In plain javascript, we would do something like
let first variable = 0
we all know this is a number variable yeah even typescript does(wouldn't want to go into that detail here) but here's the equivalent in typescript.
let firstVariable: number = 0
let firstString: string = 'My first string'
let firstBool:boolean = true
where Number, String, Boolean is a type. Yeah, why are we doing all this additional stuff?
well, I believe the moment you start trying this out in your IDE you'll discover warnings saying you can't leave a variable of type Number null etc.
Trust me this stuff would come in handy when things start getting complex. I wouldn't want to be talking about the benefits of typescript here as it is out of the scope of this post.
Now we might ask what about variables such as arrays and objects
let firstArrayOfNumbers: Array<number>
let secondArrayOfStrings: Array<string>
let thirdArrayOfBool:Array<boolean>
Now at this point, I'm sure you're having some interesting questions here and that's great. Let then fire up your research you are on the right track
what about objects?
interface MyObject {
name: string
age: number
address: string
married: boolean
}
let myFirstObject: MyObject = {
name: "Titus",
age: 30,
address: "Linux street",
married: false
}
Here you are describing each field of the object. What would be the type of each field? This comes in handy when you would want to use the variable in some other place.
Just simplly you created your own type for an object definition called MyObject.
Lastly, how do I write functions
Like I said the whole Idea of typescript is being intentional. Now when we want to write a function we ask what would this function take. , what would be the response of the function? would the parameters be strings and the returned value be a number?
what if I'm not returning anything?
function myFirstAddFunction (a:number,b:number) : number {
return a + b
}
function myFirstVoidFunction (a: number): void{
console.log(a)
}
I believe this isn't the finest of tutorials but the aim of this post is just to awaken your curiosity. Now you have questions make research and take various tutorials online without being scared and trust me you'll love the benefits that typescript comes with.
Thanks for reading
Top comments (7)
Although I would like to ask since the language intends for the developer to be intentional, what about cases where the developer cannot account for the data type from users' input?
say a form field designated as an array where the user is free to input either a string, number etc.
Inputs are strings. For you to do anything with them in other data types you have to cast them (convert).
Or even better. Properly validate the input to make sure the value is in the correct type.
thanks.
I'll try it
I understand thanks
thanks for this Mr. white
I recently had to switch from javascript to typescript and I had indeed been considering learning typescript as a course.
Even when I began using it, looking at this, I realize I have neglected to understand the major disparity between both languages as well as their syntax
Thanks for your comment.
I'm aware of you not typing everything.
It's jut to show what typing is in typescript to beginners. Thanks for the great comments.