Subscribe to DeepL Pro to edit this document.
Visit www.DeepL.com/profor more information.
What Is TypeScript
It is a modern programming language that allows you to create robust
web applications in JavaScript.
TypeScript (TS) is a programming language built on top of JavaScript
(JS).
This means that TypeScript endows the language with several
additional features that make it possible to write code that is less
buggy, simpler, more consistent and easier to test, in short, cleaner and
more robust.
TypeScript
● It is a precompiler.
● It does not require any
plugin.
Origin TypeScript
It was created by Microsoft in 2012 and, since then, its adoption has
only grown.
Especially since Google decided to adopt it as the default language for
developing with Angular.
It is intended for the development of robust applications, implementing
features in the language that allow us to develop more advanced tools.
Origin TypeScript
Main Function
We can use it to build abstraction layers, mimicking classes, hiding
information and modules. In TypeScript, however, we have the concept
of classes, namespaces, and modules, but functions are still an integral
part of describing how to do things.
Advantages:
- Code reusability
- Less coding
- Easy to debug
TypeScript
How to use it?
TypeScript is very used in tools and frameworks like Angular, React
JS, Vue JS, Node JS, etc. If you're working in a large team and need to
minimize the possible ways someone could misuse your code,
TypeScript is a good way to help fix it.
TypeScript can be used in cases such as:
- The High Performance System Problem.
- The Embedded System Problem.
- The Complex Domain Problem.
TypeScript
Size Project
For projects that will remain simple and small, or if they are known to
be scrapped over time, I'd be less inclined to recommend TypeScript as
a requirement. Lack of unit tests:
• Startup
• Teamwork
• Communicate patterns and implement design principles
frameworks
Installing TypeScript
For TypeScript installation you need to use the Node
Js package manager (NPM).
1. Open the console.
2. Type the following command.
> npm install -g typescript
Checking the installation
To check the TypeScript installation you need to use
the console and type the following command.
> tsc -v
Start with TypeScript
1. Create a new directory.
2. Add a new file with the .ts extension.
3. Open the console.
4. Type the following command to compile the
file.
> tsc app.ts
Configuration for TypeScript
1. Open the console.
2. Type the following command.
> tsc -init
This command will create a file named tsconfig.json.
Configuration for TypeScript
Example of tsconfig.json file
{
"compilerOptions" :
{ "module" :
"commonjs","target" :
"es5","noImplicitAny" :
false,"sourceMap" :
false.
}
}
Definition of variable.
In TypeScript there are two ways to declare
variables:
Let has a block range.
Var does not have a block scope.
var
Example of var.
var foo = 123;
if (true) {
var foo = 456;
}
console.log(foo);
le
t
Example of use of
let.
let foo = 123;
if (true) {
let foo = 456;
}
console.log(foo);
Types of data
TypeScript is a language that adds a static typing
layer to JavaScript and incorporates object-oriented
programming.
Primitive data: Boolean
Boolean
true or false
let access : boolean = true;
access = false;
Primitive data: Number
Number
Numerical data.
let decimal: number = 6;
let hex: number =
0xf00d;
let binary: number =
0b1010; let octal: number =
0o744;
Primitive data: String
String
Character strings and/or texts.
let name: string = 'Juan Perez';
let address: string = "Guatemala, Zone 1";
let message: string = `Hello World`;
Primitive Data: String Template
Template
They are templates for concatenating
strings.
let name: string = 'Juan Perez';
let age: number = 37;
let message: string = `Hello, my name is ${ name }.
and my age is ${ age}`;
Primitive data: Any
Any
It can be any type of javascript object.
let variable: any = 4;
variable = "maybe a string instead"; // typeof =
string variable = false; // typeof = boolean
Primitive data: Array
Array
Arrays, if no type is specified, they are ANY.
let listNumber: number[ ] = [ 1, 2, 3, 4 ];
let listString: Array<string> = ["Hello", "Greetings", "Goodbye"];
Definition of methods
Methods with mandatory
parameters.
function sum(x: number, y: number):
number{ return x + y;
}
function calculate(x: number, y: number):
void { let total: number = (x + y);
}
Definition of methods
Methods with optional parameters.
function sum(x? : number, y? : number):
number{ return x + y;
}
function calculate(x? : number, y? : number):
void { let total: number = (x + y);
}
Definition of methods
Methods with parameters and default
value.
function sum(x: number = 10, y: number = 10):
number{ return x + y;
}
function greet(x: string="Hello", y?: string): void
{ let message: string= (x + y);
}
Definition of lambda methods
Parameter methods.
let sum = (x: number, y: number) =>
{ (x + y);
}
let greet = (name: string) =>
{ "Hello " + name;
}
Object Oriented
Programming
with
TypeScript
Example of a TypeScript class
class Person {
public name: string;
constructor(name: string) {
this.name = name;
Classes }
greet(): string{
return "Hello, I am " + this.name;
}
}
Example for instantiating a class
Instance let persona = new Person("John");
TypeScript also supports access or
Access visibility modifiers.
● public
modifiers ● private
● protected
Example of inheritance in
TypeScript
class Person {
public name: string;
constructor(name: string) {
this.name = name;
Heritage }
show(phone: string) { console.log(`$
{this.name} and the number $
{phone}.`);
}
}
CONCLUSION
TypeScript is used to develop JavaScript applications
that will run on the client or server side, or
extensions for programs (Node.js and Deno).
TypeScript extends the syntax of JavaScript, so any
existing JavaScript code should work without
problems.
REFERENCES
https://codigofacilito.com/articulos/typescript
https://www.typescriptlang.org/
Conclusions