TypeScript | TypeORM

Jay·2022년 5월 17일
0

What is TypeScript ?

As the name implies, indeed TypeScript is a superset of JavaScript. In fact, TypeScript programs (.ts) can be compiled down to JavaScript (.js) programs. It uses JavaScript syntax but has additional features; said differently, all JavaScript progams are TypeScript programs. TypeScript uses that same JavaScript syntax but has additional features specified in the language.

Why TypeScript ?

TypeScript has additional features to simplify and avoid bugs when writing programs. Some examples include strict typing, interfaces, mixin classes, and enum types. More importantly, unlike JS - where a bug can only be detected during runtime - TypeScript checks for issues during compile time. This is important, especially when these issues are carried over to the final program, disturbing user experience.

1 : Type Systems

TypeScript implements a strict type system, like Java: boolean, string, number, void, null, undefined. Although it may seem uncomfortable at first, such practices greatly reduces unnecessary errors. Apart from this, a union type can be used to combine and use two types for the same variable.

2 : Interfaces

TypeScript supports interfaces, useful in describing primary schema for objects (interface meaning the keys and their types declared but not initiated). This is an important feature absent in JavaScript.

Example Interface for Objects:

interface Student {
	name: string; // key "name" is of type String
	present: boolean; // key "present" is of type Boolean
}

let obj: Student;

Example Interface for Classes:

interface Dog {
	type: string; // dog type is of type String
	bark: () => void; // bark is a type of void function
}

class Class implements Dog {
	type: string;

	bark() {
		return;
	}
}

3 : Enum Types

TypeScript enums list all possible values it can hold, making it easier to document distinct cases.

enum School {
	Primary,
	Middle,
	High,
	College,
}

function checkNumber(school: School) {
	// ...
}

checkNumber(School.Primary);
checkNumber(School.Middle);

To achieve the same type of result in JavaScript, it would require the developer to make a new object type of school.

Now that you're ready to write code in TypeScript and JavaScript, as a helpful side note let's talk about linting.

Linting : Using tools that will go over your written code, cleaning up and organizing them

Simply said, linting tools clean up messy code and catch unnecessary errors and mistakes. In this aspect, lintng is important because it can greatly reduce the time spent on debugging and makes coding easier. There are multiple ways of linting, some being linting manually in browser, code editor, command line, part of build process, etc. But one of the most popular linters is Eslint.

Prettier serves a similar function as a linter, but different in that rather than identifying style errors in code, it focuses on the overall layout of the code. This can include use of indents, spaces, and line breaks.

TypeORM

Now image building a Typescript-based application. Often, the server computer will be connected to the frontend computer but also the database computer to store and fetch relevant information. Linking the server and database is what is called the ORM (object-relational mapper). As the name suggests, ORM links the server to a relational database, which makes use of rows and columns (main example being MySQL).

The general purpose of the ORM becomes clear when thinking about the fundamentals behind the language and database systems. Object-oriented programming languages make use of classes, and relational databases use tables, much like the familiar excel spreadsheet. Clearly, there is some clear structural differences between object-oriented and relational models. What resolves these differences is the ORM, which 'maps' and constructs a relational database based on the relationship between different objects.

The main example such ORM used to link Express and a relational database is "Sequelize", and one which is rapidly gaining popularity (due to frequently used TypeScript) is TypeORM. The name may be misleading in that TypeORM actually supports more than just TypeScript, but also different versions of JavaScript.

0개의 댓글