An Interest In:
Web News this Week
- March 2, 2024
- March 1, 2024
- February 29, 2024
- February 28, 2024
- February 27, 2024
- February 26, 2024
- February 25, 2024
Understanding Typescript
A JavaScript superset, that builds up on JavaScript language, It adds new features to JavaScript, typescript cant run in the browser or even in NodeJS therefore it has a compiler that compiles typescript to JavaScript. Added features in typescript include the use of types, it also helps us be more explicit and clearer about our code.
Lets look at two code snippets one with Plain Javascript and another in typescript .Plain Javascript
let inputA = 1 let inputB = 2 function sum(inputA, inputB){ console.log(inputA + inputB) } sum(inputA,inputB) //this will return 3 //Now what if we change the inputs to be inputA = "1" inputB = "2" sum(inputA,inputB) //this will return 12
Despite the second answer being wrong, we dont get an error notification at run time. Here is when typescript comes in with its additional type feature that guarantees we get the correct answer or an error.In typescript
this would be written as(you can test this code in the TS playground):
let inputA = 1 let inputB = 2 funtion sum(inputA: number,inputB: number){ console.log(inputA + inputB) } sum(inputA,inputB) //prints 3 //Now what if we change the inputs to be inputA = "1" inputB = "2" sum(inputA,inputB) //script.ts(6,9): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
Through the introduction of types in typescript, it easier to write more intuitive code. I will be writing a series of tutorial on typescript.
I will cover:
- Typescript Basics
- Typescript compiler
- Classes & Interfaces
- Advanced Typescript features such as Generics & Decorators
Stay tuned !!
Original Link: https://dev.to/bazeng/understanding-typescript-5gch
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To