Skip to content

Commit 60a5fb4

Browse files
authored
Merge pull request mouredev#7667 from duendeintemporal/main
#2 - typescript
2 parents badffc6 + 1695822 commit 60a5fb4

File tree

3 files changed

+1362
-0
lines changed

3 files changed

+1362
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// #00 { retosparaprogramadores } SINTAXIS, VARIABLES, TIPOS DE DATOS Y HOLA MUNDO
2+
/*
3+
* ¿Preparad@ para aprender o repasar el lenguaje de programación que tú quieras?
4+
* - Recuerda que todas las instrucciones de participación están en el
5+
* repositorio de GitHub.
6+
*
7+
* Lo primero... ¿Ya has elegido un lenguaje?
8+
* - No todos son iguales, pero sus fundamentos suelen ser comunes.
9+
* - Este primer reto te servirá para familiarizarte con la forma de participar
10+
* enviando tus propias soluciones.
11+
*
12+
* EJERCICIO:
13+
* - Crea un comentario en el código y coloca la URL del sitio web oficial del
14+
* lenguaje de programación que has seleccionado.
15+
* - Representa las diferentes sintaxis que existen de crear comentarios
16+
* en el lenguaje (en una línea, varias...).
17+
* - Crea una variable (y una constante si el lenguaje lo soporta).
18+
* - Crea variables representando todos los tipos de datos primitivos
19+
* del lenguaje (cadenas de texto, enteros, booleanos...).
20+
* - Imprime por terminal el texto: "¡Hola, [y el nombre de tu lenguaje]!"
21+
*
22+
* ¿Fácil? No te preocupes, recuerda que esta es una ruta de estudio y
23+
* debemos comenzar por el principio.
24+
*/
25+
26+
// TypeScript - Official URL: https://www.typescriptlang.org/
27+
28+
/* by @duendeintemporal */
29+
30+
// One-line comments
31+
/* Comments
32+
of
33+
many lines */
34+
35+
/* I set my environment in the following way:
36+
1. First, run:
37+
npm init -y
38+
2. Then, install TypeScript and ts-node globally:
39+
npm install -g typescript ts-node
40+
3. Now I can run my TypeScript files directly using:
41+
ts-node ./src/my_file.ts
42+
*/
43+
44+
45+
let lang: string; // Variable to hold the programming language
46+
let num: number; /*
47+
The 'let' keyword declares a variable with block scope.
48+
Unlike 'var' in JavaScript, 'let' does not attach to the window object in the global context.
49+
*/
50+
51+
const MIN_VA: number = 0; /*
52+
'const' declares a constant with block scope and must be initialized with a value.
53+
By convention, constants are usually named using capital letters.
54+
*/
55+
56+
// Primitive Types or Primitive Values
57+
58+
let hello: string = 'Hi Girl!'; // string type
59+
console.log(hello); // Hi Girl!
60+
console.log(`String type: ${hello}`, typeof hello); // String type: Hi Girl! string
61+
62+
let x_coord: number = 100; // number type
63+
console.log(x_coord); // 100
64+
console.log('Number type: ', typeof x_coord); // Number type: number
65+
66+
let bool: boolean = true; // boolean type
67+
console.log(bool); // true
68+
console.log('Boolean type: ', typeof bool); // Boolean type: boolean
69+
70+
let stack: number[] = [0, 1, 2, 3, 4, 5, 6]; // array type
71+
console.log(stack); // [ 0, 1, 2, 3, 4, 5, 6 ]
72+
console.log('Array type: ', typeof stack); // Array type: object
73+
74+
let obj: { name: string; age: number; profession: string; greetings: () => void } = {
75+
name: 'Niko',
76+
age: 41,
77+
profession: 'Writer & Web Developer',
78+
// method
79+
greetings: function () {
80+
console.log(`Hello, I am ${this.name} and it's a pleasure to start and share this roadmap with you !!`);
81+
}
82+
}; // object type
83+
84+
console.log(obj);
85+
/*
86+
Object type: {
87+
name: 'Niko',
88+
age: 41,
89+
profession: 'Writer & Web Developer',
90+
greetings: [Function: greetings]
91+
} */
92+
console.log('Object type: ', typeof obj); // Object type: object
93+
obj.greetings(); // Hello, I am Niko and it's a pleasure to start and share this roadmap with you !!
94+
95+
let tuple: [string, number, boolean, { webmaster: string }] = ['Soe', 35, true, { webmaster: 'Niko' }];
96+
console.log(tuple); // [ 'Soe', 35, true, { webmaster: 'Niko' } ]
97+
console.log('Tuple type: ', typeof tuple); // Tuple type: object
98+
99+
enum Color {
100+
Red,
101+
Green,
102+
Blue,
103+
Black,
104+
Purple
105+
}
106+
console.log('Enum type: ', typeof Color); // Enum type: object
107+
let fontColor: Color = Color.Green;
108+
console.log('Enum type: ', typeof fontColor); // Enum type: number
109+
110+
111+
let obj2: null = null;
112+
/*
113+
Represents the absence of a value, helping to handle situations where a value is not available
114+
or has not been intentionally defined.
115+
*/
116+
console.log('Null type: ', typeof obj2); // Null type: object
117+
118+
let obj3: undefined; // undefined type
119+
/*
120+
'undefined' indicates that a variable has been declared but not initialized.
121+
*/
122+
console.log('Undefined type: ', typeof obj3); // Undefined type: undefined
123+
124+
let anything: any = "I can be anything!";
125+
console.log('Any type: ', typeof anything); // Any type: string
126+
anything = 42; // Now it's a number type
127+
console.log('Any type after reassignment: ', typeof anything); // Any type after reassignment: number
128+
129+
130+
let syn: symbol = Symbol('syn'); // symbol type
131+
console.log('Symbol type: ', typeof syn); // Symbol type: symbol
132+
/*
133+
Symbols can be used to prevent object collisions, such as creating hidden non-enumerable properties
134+
on objects or private methods in a class.
135+
*/
136+
137+
let amount: bigint = BigInt(3783787487877877887) * BigInt(2); // bigint type
138+
console.log(amount) // 7567574975755755520n
139+
console.log(`BigInt type: ${amount}`, typeof amount); // BigInt type: 7567574975755755520 bigint
140+
/*
141+
Using BigInt is especially useful in situations where accuracy is required in calculations
142+
with large integers, such as in financial or crypto applications.
143+
*/
144+
145+
/*
146+
'typeof' is an operator that we can use to determine a type or make type comparisons.
147+
*/
148+
console.log('Is amount of bigint type:', (typeof amount === "bigint")); // Is amount of bigint type: true
149+
150+
// Short for console.log()
151+
let log = console.log.bind(console);
152+
153+
// Function with type annotations
154+
function add(a: number, b: number): number {
155+
return a + b;
156+
}
157+
log('Function result: ', add(25, 18)); // Function result: 43
158+
159+
// Interfaces in Typescript are use to define a type with properties.
160+
interface Person {
161+
name: string;
162+
age: number;
163+
}
164+
165+
let person: Person = {
166+
name: 'Bob',
167+
age: 34
168+
};
169+
log('Interface type: ', typeof person); // object
170+
171+
// Ejemple of Type Assertion
172+
let someValue: any = "this is a example string";
173+
let strLength: number = (someValue as string).length;
174+
log('String length: ', strLength); // String length: 24
175+
176+
// Arrow function with type annotations and Promises
177+
const fetchData = (): Promise<string> => {
178+
return new Promise((resolve) => {
179+
setTimeout(() => {
180+
resolve("Data fetched!");
181+
}, 2000);
182+
});
183+
};
184+
185+
fetchData().then(data => log(data)); // Data fetched! (after 2 seconds)
186+
187+
// Assigning a new value to a previously declared variable
188+
lang = 'Typescript';
189+
190+
// Print in console
191+
log(`Hello, ${lang}`); // Hello, Typescript

0 commit comments

Comments
 (0)