Fundamentos Typescript
Fundamentos Typescript
Bagde
del curso
Fundamentos
de TypeScript
Luis Aviles
@luixaviles
El Lenguaje de
Programación
TypeScript
¿Qué es TypeScript?
typescriptlang.org
¿Qué es TypeScript?
● Código Abierto
● Desarrollo en cualquier Sistema
Operativo
● El código puede ejecutarse en
cualquier navegador o plataforma
¿Qué es TypeScript?
Código válido
¿Qué es TypeScript?
¿Qué es TypeScript?
Racionales
-8/3 6.15
-10 8
Naturales
1, 2, 3, ...
¿Quién usa TypeScript?
2393+ compañías
https://stackshare.io/typescript
¿Quién usa TypeScript?
[1] https://github.com/microsoft/TypeScript/wiki/Roadmap
¿Por qué usar TypeScript?
http://ttendency.cs.ucl.ac.uk/projects/type_study/documents/type_study.pdf
¿Por qué usar TypeScript?
Cliente
Servidor
Instalación de
Herramientas
Instalación de Herramientas
Instalación de
Herramientas
Windows
Instalación de
Herramientas
macOS
Navegación y
Refactorización
El Compilador
de TypeScript
tsc: TypeScript Compiler
Instalación de tsc
Compilador de TypeScript
hello.ts hello.js
$ tsc hello.ts
Usando el compilador tsc
$ tsc hello.ts
$ ls
Usando el compilador tsc
$ tsc hello.ts
$ ls
hello.js hello.ts
Usando el compilador tsc
$ tsc hello.ts
$ ls
hello.js hello.ts
$ node hello.js
Usando el compilador tsc
$ tsc hello.ts
$ ls
hello.js hello.ts
$ node hello.js
hello Platzi world!
Usando el compilador tsc
La opción --watch
$ tsc --init
Generación del archivo
tsconfig.json
$ tsc --init
message TS6071: Successfully created a tsconfig.json file.
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"removeComments": true
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"removeComments": true
},
"include": [
"src/**/*.ts"
]
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"removeComments": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.test.ts"
]
}
tsconfig.json
{
"extends": "./configs/base",
"compileOnSave": true,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"removeComments": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.test.ts"
]
}
Usando el archivo
tsconfig.json
$ tsc
$ tsc file.ts
Usando el archivo
tsconfig.json
● Explícito
Define una sintaxis para la creación de
variables con tipo de dato.
● Inferido
TypeScript tiene la habilidad de
“deducir” el tipo en función de un valor.
Tipado en TypeScript
Explícito
Permite especificar el
tipo de dato
Tipado en TypeScript
Inferido
nombreVariable = Valor
Tipado en TypeScript
Implícito
nombreVariable = Valor
● Valores numéricos
● Valores hexadecimales
● Valores binarios
● Valores octales
Tipo: Boolean
"strict": true
"strict": false
"strictNullChecks": true
"strictNullChecks": false
Object
Tipo: object
Testeable Ordenado
Encapsulado
Importando y
Exportando en
Módulos
Importando y Exportando en
Módulos
classic node
Módulos AMD, System, Módulos CommonJS o UMD
ES2015
Poco configurable Más opciones de
configuración
"module": "<valor>"(tsconfig.json)
Classic: Import Relativo
// Archivo: /typescript/photo-app/main.ts
import { Picture } from ‘./picture;
/typescript/photo-app/picture.ts
/typescript/photo-app/picture.d.ts
Classic: Import No-Relativo
// Archivo: /typescript/photo-app/main.ts
import { Picture } from ‘picture’;
/typescript/photo-app/picture.ts
/typescript/photo-app/picture.d.ts
/typescript/picture.ts
/typescript/picture.d.ts
(continúa buscando en el árbol de directorios)
Node: Import Relativo
// Archivo: /typescript/photo-app/main.ts
import { Picture } from ‘./picture;
/typescript/photo-app/picture.ts
/typescript/photo-app/picture.tsx
/typescript/photo-app/picture.d.ts
Node: Import Relativo
// Archivo: /typescript/photo-app/main.ts
import { Picture } from ‘./picture;
/typescript/photo-app/picture.ts
/typescript/photo-app/picture.tsx
/typescript/photo-app/picture.d.ts
/typescript/photo-app/picture/package.json (“typings”)
Node: Import Relativo
// Archivo: /typescript/photo-app/main.ts
import { Picture } from ‘./picture;
/typescript/photo-app/picture.ts
/typescript/photo-app/picture.tsx
/typescript/photo-app/picture.d.ts
/typescript/photo-app/picture/package.json (“typings”)
/typescript/photo-app/index.ts
/typescript/photo-app/index.tsx
/typescript/photo-app/index.d.ts
Node: Import No-Relativo
// Archivo: /typescript/photo-app/main.ts
import { Picture } from ‘picture’;
/typescript/photo-app/node_modules/picture.ts
/typescript/photo-app/node_modules/picture.tsx
/typescript/photo-app/node_modules/picture.d.ts
/typescript/photo-app/node_modules/picture/package.json
/typescript/photo-app/node_modules/index.ts
Node: Import No-Relativo
// Archivo: /typescript/photo-app/main.ts
import { Picture } from ‘picture’;
/typescript/photo-app/node_modules/picture.ts
/typescript/photo-app/node_modules/picture.tsx
/typescript/photo-app/node_modules/picture.d.ts
/typescript/photo-app/node_modules/picture/package.json
/typescript/photo-app/node_modules/index.ts
/typescript/node_modules/picture.ts
/typescript/node_modules/picture.tsx
/typescript/node_modules/picture.d.ts
/typescript/node_modules/index.ts
/typescript/node_modules/index.tsx
/typescript/node_modules/index.d.ts
$ npm init -y
Instalación de TypeScript y
Webpack
module.exports = {
mode: 'production',
entry: './main.ts',
devtool: 'inline-source-map',
resolve: {
extensions: ['.ts', '.js'],
},
output: {
filename: 'bundle.js',
}
}
Instalación de ts-loader
module.exports = {
mode: 'production',
entry: './src/main.ts',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.ts', '.js'],
},
output: {
filename: 'bundle.js',
}
}
// Estructura de archivos
|- ts-project/
|- src/
|- main.ts
|- photo-app/
|- index.ts
|- dist/
index.html
bundle.js
|- package.json
|- tsconfing.json
|- webpack.config.js
Cierre del Curso