Skip to content

Commit 44e62c5

Browse files
committed
update ts advanced
1 parent 7293d73 commit 44e62c5

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ const myTree: TreeNode<number> = {
368368

369369
// Aunque aplicando la recursividad en interfaces si que podíamos hacer cosas muy interesantes
370370
// como esta:
371-
type IterableList<T> = T & { next: IterableList<T> | null };
371+
type IterableList<T extends object> = T & { next: IterableList<T> | null };
372372

373373
interface Student {
374374
name: string;
@@ -422,14 +422,11 @@ const myTreeMM: TreeNodeMM<string> = ["hello", [["world"], "!"]];
422422
// introduce nuevos tipos genéricos ineridos sobre la marcha: la keyword "infer".
423423

424424
// EJEMPLO MÁS SENCILLO SIN RECURSIVIDAD ***************
425-
type IsStringArray<T extends any[]> = T extends Array<infer Items>
426-
? Items extends string
427-
? true
428-
: false
429-
: never;
425+
type IsPrimitiveArray<T extends any[]> = T extends (infer Type)[] ? Type extends object? false : true : never;
430426

431-
type Result1 = IsStringArray<[1, 2]>; // false
432-
type Result2 = IsStringArray<["hello", "world"]>; // true
427+
type Result1 = IsPrimitiveArray<[1, 2]>;
428+
type Result2 = IsPrimitiveArray<["a", "b"]>;
429+
type Result3 = IsPrimitiveArray<[{}]>;
433430

434431
// EJEMPLO CON RECURSIVIDAD ****************************
435432
type RemoveZeroes<T extends any[]> = T extends [infer Head, ...infer Tail]
@@ -438,4 +435,13 @@ type RemoveZeroes<T extends any[]> = T extends [infer Head, ...infer Tail]
438435
: [Head, ...RemoveZeroes<Tail>]
439436
: T;
440437

441-
type Result3 = RemoveZeroes<[0, 1, true, 0, 2, 0, "hello", 0, {}]>;
438+
type Result4 = RemoveZeroes<[0, 1, true, 0, 2, 0, "hello", 0, {}]>;
439+
440+
// Y una implementación básica, podría ser:
441+
442+
// Fijaos como he añadido el genérico T como const para que lo interprete como una tupla.
443+
const removeZeroes = <const T extends any[]>(array: T): RemoveZeroes<T> =>
444+
array?.filter(x => x !== 0) as any;
445+
446+
// Mira intellisense encima de 'result'
447+
const result = removeZeroes([0, 1, true, "hello", 0, {name: "Santi"}, 0]);

02-languages/02-apuntes/02-typescript/107 utility types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Point3D {
116116
constructor(coord: Coord) {
117117
this.coord = {
118118
...coord,
119-
z: coord.z || 0,
119+
z: coord.z ?? 0,
120120
};
121121
}
122122
getZ() {
@@ -272,6 +272,10 @@ type MyPick<T, K extends keyof T> = {
272272
[P in K]: T[P];
273273
};
274274
type MyOmit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
275+
// Alternative
276+
// type MyOmit<T extends object, K extends keyof T> = {
277+
// [P in Exclude<keyof T, K>]: T[P]
278+
// }
275279

276280
// *** RECORD ***************
277281

0 commit comments

Comments
 (0)