TypeScript es un lenguaje de programación desarrollado y mantenido por Microsoft. Es un superconjunto de JavaScript que añade tipos estáticos opcionales.
Para instalar TypeScript, puedes usar npm:
npm install -g typescriptPara compilar archivos TypeScript (.ts), usa el comando tsc:
tsc archivo.tsPuedes crear un archivo tsconfig.json para configurar el compilador TypeScript:
tsc --init"include": [
"ts/**/*" // Incluye todos los archivos .ts en la carpeta 'ts'
]Debes ejecutar el siguiente comando
tsclet isDone: boolean = false;let decimal: number = 6;let color: string = "blue";let list: number[] = [1, 2, 3];let x: [string, number];
x = ["hello", 10];enum Color {Red, Green, Blue}
let c: Color = Color.Green;let notSure: any = 4;
notSure = "maybe a string instead";function warnUser(): void {
console.log("This is my warning message");
}interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
let user = { firstName: "Jane", lastName: "User" };
console.log(greeter(user));class Animal {
private name: string;
constructor(name: string) {
this.name = name;
}
public move(distanceInMeters: number): void {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
let dog = new Animal("Dog");
dog.move(10);// math.ts
export function add(x: number, y: number): number {
return x + y;
}
// app.ts
import { add } from "./math";
console.log(add(2, 3));TypeScript es una poderosa herramienta para desarrollar aplicaciones JavaScript robustas y mantenibles. Esta guía cubre los conceptos básicos, pero hay mucho más por explorar.