- What is TypeScript & Why use it?
- Setting up TypeScript.
- Examples Static Typing & Errors in TypeScript.
What is Typescript and Why Would I use it?
Now you wont be able to shoot lasers after installing typescript; however, typescript’s super power is its ability to add static typing to your code. You will catch errors as you type, saving you the heart ache of seeing red line after red line of errors after running your file.
Setting up Typescript for the First Time (VS Code):
1. mkdir ts-practice -> code . (creates practice directory and opens it)2. npm init -y (gives us package.json to download node modules)3. npm install typescript (installs module)4. touch index.ts (creates our first typescript file)
In any directory you want follow the commands above to get your first typescript environment set up.
Command + Shift + p
(control for Operating systems)
Search for “default settings”, type in “typescript.validate”, and make sure that this section is set to true. This is very important for seeing errors later down the development line… and conveniently for the next section.
Static Typing Fundamental's/ Error Examples
With the TypeScript environment set up, lets discuss static typing. When declaring and using variables in T.S you are able to add regulations for each data type. An error will show immediately in your IDE if not done correctly.
As you can see the variables above have a pre-existing ruleset/type that will provide an error(red squiggly) if not followed. Debugging in this manner is favorable because we do not have to wait for results like we would with Dynamic typing.