Skip to content

Commit 2d9c782

Browse files
committed
update module 2 advanced types ts
1 parent 33d4aa7 commit 2d9c782

1 file changed

Lines changed: 10 additions & 22 deletions

File tree

02-languages/02-apuntes/02-typescript/106 advanced types.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,7 @@ const day: WorkingDay = "sunday"; // TS error: '"sunday"' is not assignable to t
249249

250250
// También es aplicable a números literales:
251251
type Dice = 1 | 2 | 3 | 4 | 5 | 6;
252-
const throwDice = (): Dice => {
253-
const randomNum = Math.ceil(Math.random() * 6);
254-
return (randomNum !== 0 ? randomNum : 1) as Dice;
255-
// return 6; // Dado trucado MUAHAHAHAHA.
256-
};
252+
const throwDice = (): Dice => Math.floor(Math.random() * 6 + 1) as Dice;
257253

258254

259255
// *** TEMPLATE LITERALS ************************
@@ -275,28 +271,20 @@ type MyDayWithScale = `${DayMood} ${WorkingDay} - Level ${1 | 2 | 3}`;
275271
// tipo concreto:
276272

277273
// -- Caso Base --
278-
interface Week {
279-
monday: string;
280-
tuesday: string;
281-
wednesday: string;
282-
thursday: string;
283-
friday: string;
284-
saturday: string;
285-
sunday: string;
274+
interface Person {
275+
name: string;
276+
age: number;
277+
adult: boolean;
286278
}
287-
type Day = keyof Week;
279+
type PersonKey = keyof Person; // type PersonKey = "name" | "age" | "adult";
288280

289281
// -- Caso Práctico --
290-
const showProps = <O extends object>(obj: O, ...keys: (keyof O)[]): void => {
291-
keys.forEach((key) => console.log(obj[key]));
282+
const showProps = <O extends object>(obj: O, ...keys: (keyof O)[]) => {
283+
keys.forEach(key => console.log(key, obj[key]));
292284
};
293285

294-
const dev = {
295-
type: "frontend",
296-
languages: ["js", "css", "html"],
297-
senior: true,
298-
};
299-
showProps(dev, "type", "languages"); // Check intellisense!;
286+
const javi: Person = { name: "Javi", age: 41, adult: true };
287+
showProps(javi, "adult", "color"); // Check intellisense!;
300288

301289

302290
// *** TIPOS MAPEADOS (MAPPED TYPES) ***************

0 commit comments

Comments
 (0)