A Quick Look into Typescript
What is a type script? Today we are going to talk about what typescript is and why it is typescript
TypeScript is a language by Microsoft built on top of JavaScript. Unlike JavaScript, it’s a strongly typed language.
It has all the features and functionalities of JavaScript, which makes it a superset of JavaScript.
Why TypeScript?
TypeScript provides static typing and improves the developer experience by pointing out compilation errors during development. This makes runtime errors less likely.
JavaScript is a dynamic language and type conversion may cause some expected results and behaviors. TypeScript helps us to avoid those unexpected behaviors.
//CODE. User is a string, it can’t be converted to an integer
let user = “Tassu”;
user = 99; // Returns error
The Syntax.
TypeScript’s syntax is the same as JavaScript’s. Here’s an example of how to create an object in TypeScript.
//CODE
const user = {
firstName: “Tassu”, LastName: “Ali”, role: “MERN Stack”,
}
console.log(user.name) // Return error
// Name doesn’t exist in the user object
Initiate a Project
To initiate a TypeScript project, unlike React.js, we don’t need to set up many things.
All we need is to install TypeScript’s npm package.
//CODE
npm install -g typescript
Next, we create a file with a .ts extension in a folder.
//CODE
function greeter (person: string) {
}
return “Hello,” + person;
let user “Tassu User”;
console.log(user);
Compile TypeScript
TypeScript is a language built on top of JavaScript, but browsers don’t understand it, which is why the TypeScript code has to be converted to JavaScript. We can do so by using the following command.
//CODE
tsc index.ts
This will create a Javascript version of the code.
What’s Next?
That’s the very basics of TypeScript. We have many resources available on the internet to learn more.
One of the most helpful places to start is the TypeScript docs itself.
https://www.typescriptlang.org/docs/
If you need any help or guidance about the coding/programming life just ping me on Instagram at https://www.instagram.com/codeculturepro/ or on Gmail at codeculturepro@gmail.com
