TypeScript Crash Course
Developed and maintained by Microsoft. TypeScript is JavaScript with syntax for types. Similar to JavaScript, the difference is TypeScript is strict, especially to data type. TypeScript needs to compile first. It will produce JavaScript files for production.
We should master JavaScript before TypeScript. It is important. They use the same syntaxes, but TypeScript adds additional syntaxes to make it strict. You can visit JavaScript Crash Course the learn JavaScript first.
Prerequisites
I assume you already installed prerequisites from here, they are the text editor and NodeJS. Next is to install TypeScript. I suggest to install it globally via npm (NodeJS package manager), npm install -g typescript
. You can verify it by tsc --version
. Type tsc --help
to show all information. Type tsc --init
to create a tsconfig.json
file. It will leave you a recommended config. It is to configure the TypeScript compiler, especially when in the editor, to remove errors in the editor that caused by conflicts with the JavaScript file. You can visit this page for more information.
Create a main.ts
file. Run tsc --watch main.ts
on the terminal. It will create a main.js
file and recreate it every time it has change. You can play around with some examples below. Practice makes perfect.
Type
TypeScript is strict about data type. The variable can't changes its data type except any
. Here are built-in type primitives; boolean
, string
, number
, undefined
, null
, any
, unknown
, never
, void
, bigint
, and symbol
. In TypeScript, we can define a primitive type by a syntax type
. I made examples of type
to explain its rule.
Object Literal Type
|
|
With union |
, we can define options for the value.
Tuple Type
|
|
Tuple is a fixed array with its fixed data type.
Intersection Types
|
|
We can combine types by using &
.
Type Indexing
|
|
We can get the data type by indexing.
Type from Value
|
|
We can get the data type from the function by using ReturnType
then assign it.
Mapped Types
|
|
We can create a mapped data type for functions. This feature is great for building libraries.
Conditional Types
|
|
We can create a condition to reduce options for data types.
Interface
Interface is to describe the shape of objects. Common built-in objects; Date
, Error
, Array
, Map
, Set
, Regexp
, and Promise
. In JavaScript and TypeScript, we can create templates for objects by using classes. Maybe I will make an article for that later.
Generics
|
|
This interface
has the same function as the type
above.
Extending Interface
|
|
The extends
is for extending interface
.
Intersection Interface
|
|
We also can extend or combine interfaces by using &
.
Optional & Readonly Properties
|
|
The ?
is to let the attribute to be empty or undefined
. The readonly
is to forbid value modification.
Interface with Parameters (Generics)
|
|
The interface
can have parameters. The parameter is the data type. It is similar to the generic one above.
Closing
TypeScript is designed for the development of large applications. Strict to its data type, it is important to have when working with a big number of developers and features. So, the data can be consistent with its data type.
Especially on the front-end. We can also work with TypeScript for front-end development. Many libraries support TypeScript, such as Angular and React. Many companies migrated their front-end development to TypeScript. It is a demanded skill because of its benefit or features. You can get the examples above on here, https://github.com/aristorinjuang/ts-crash-course.
References
Related Articles
- JavaScript Crash Course
- React with TypeScript
- Concurrency in Go (Part 2)
- Deploy a Go App to AWS EC2
- A Simple Blog with Go