From f40f2341045baa00dcff759d190fd8b46a4854c9 Mon Sep 17 00:00:00 2001 From: Matt Pocock Date: Tue, 15 Aug 2023 11:47:01 +0100 Subject: [PATCH] Added exercise on combining unions --- .../039-combining-unions.problem.ts | 21 ++++++++++++++++ .../039-combining-unions.solution.ts | 24 +++++++++++++++++++ ...> 040-how-big-can-a-union-be.explainer.ts} | 0 ... 041-literals-vs-wider-types.explainer.ts} | 0 ...narrowing-unions-with-typeof.explainer.ts} | 0 ...3-narrowing-with-if-statements.problem.ts} | 0 ...arrowing-with-if-statements.solution.1.ts} | 0 ...arrowing-with-if-statements.solution.2.ts} | 0 ...arrowing-with-if-statements.solution.3.ts} | 0 ...arrowing-with-if-statements.solution.4.ts} | 0 ...arrowing-with-if-statements.solution.5.ts} | 0 ...owing-with-boolean-wont-work.explainer.ts} | 0 ... 045-throwing-errors-to-narrow.problem.ts} | 0 ...045-throwing-errors-to-narrow.solution.ts} | 0 ...ing-with-instanceof-statements.problem.ts} | 0 ...ng-with-instanceof-statements.solution.ts} | 0 ...7-narrowing-with-in-statements.problem.ts} | 0 ...-narrowing-with-in-statements.solution.ts} | 0 ...8-narrowing-unknown-to-a-value.problem.ts} | 0 ...-narrowing-unknown-to-a-value.solution.ts} | 0 ...> 049-narrowing-with-ternaries.problem.ts} | 0 ... 049-narrowing-with-ternaries.solution.ts} | 0 ...rowing-with-nullish-coalescing.problem.ts} | 0 ...owing-with-nullish-coalescing.solution.ts} | 0 ...-intro-to-discriminated-unions.problem.ts} | 0 ...intro-to-discriminated-unions.solution.ts} | 0 ...ucturing-a-discriminated-union.problem.ts} | 0 ...cturing-a-discriminated-union.solution.ts} | 0 ...-union-with-a-switch-statement.problem.ts} | 0 ...union-with-a-switch-statement.solution.ts} | 0 ...4-narrowing-with-switch-true.explainer.ts} | 0 ...ucturing-a-discriminated-tuple.problem.ts} | 0 ...cturing-a-discriminated-tuple.solution.ts} | 0 ... => 056-discriminated-booleans.problem.ts} | 0 ...=> 056-discriminated-booleans.solution.ts} | 0 35 files changed, 45 insertions(+) create mode 100644 src/038-unions-and-narrowing/039-combining-unions.problem.ts create mode 100644 src/038-unions-and-narrowing/039-combining-unions.solution.ts rename src/038-unions-and-narrowing/{039-how-big-can-a-union-be.explainer.ts => 040-how-big-can-a-union-be.explainer.ts} (100%) rename src/038-unions-and-narrowing/{040-literals-vs-wider-types.explainer.ts => 041-literals-vs-wider-types.explainer.ts} (100%) rename src/038-unions-and-narrowing/{041-narrowing-unions-with-typeof.explainer.ts => 042-narrowing-unions-with-typeof.explainer.ts} (100%) rename src/038-unions-and-narrowing/{042-narrowing-with-if-statements.problem.ts => 043-narrowing-with-if-statements.problem.ts} (100%) rename src/038-unions-and-narrowing/{042-narrowing-with-if-statements.solution.1.ts => 043-narrowing-with-if-statements.solution.1.ts} (100%) rename src/038-unions-and-narrowing/{042-narrowing-with-if-statements.solution.2.ts => 043-narrowing-with-if-statements.solution.2.ts} (100%) rename src/038-unions-and-narrowing/{042-narrowing-with-if-statements.solution.3.ts => 043-narrowing-with-if-statements.solution.3.ts} (100%) rename src/038-unions-and-narrowing/{042-narrowing-with-if-statements.solution.4.ts => 043-narrowing-with-if-statements.solution.4.ts} (100%) rename src/038-unions-and-narrowing/{042-narrowing-with-if-statements.solution.5.ts => 043-narrowing-with-if-statements.solution.5.ts} (100%) rename src/038-unions-and-narrowing/{043-narrowing-with-boolean-wont-work.explainer.ts => 044-narrowing-with-boolean-wont-work.explainer.ts} (100%) rename src/038-unions-and-narrowing/{044-throwing-errors-to-narrow.problem.ts => 045-throwing-errors-to-narrow.problem.ts} (100%) rename src/038-unions-and-narrowing/{044-throwing-errors-to-narrow.solution.ts => 045-throwing-errors-to-narrow.solution.ts} (100%) rename src/038-unions-and-narrowing/{045-narrowing-with-instanceof-statements.problem.ts => 046-narrowing-with-instanceof-statements.problem.ts} (100%) rename src/038-unions-and-narrowing/{045-narrowing-with-instanceof-statements.solution.ts => 046-narrowing-with-instanceof-statements.solution.ts} (100%) rename src/038-unions-and-narrowing/{046-narrowing-with-in-statements.problem.ts => 047-narrowing-with-in-statements.problem.ts} (100%) rename src/038-unions-and-narrowing/{046-narrowing-with-in-statements.solution.ts => 047-narrowing-with-in-statements.solution.ts} (100%) rename src/038-unions-and-narrowing/{047-narrowing-unknown-to-a-value.problem.ts => 048-narrowing-unknown-to-a-value.problem.ts} (100%) rename src/038-unions-and-narrowing/{047-narrowing-unknown-to-a-value.solution.ts => 048-narrowing-unknown-to-a-value.solution.ts} (100%) rename src/038-unions-and-narrowing/{048-narrowing-with-ternaries.problem.ts => 049-narrowing-with-ternaries.problem.ts} (100%) rename src/038-unions-and-narrowing/{048-narrowing-with-ternaries.solution.ts => 049-narrowing-with-ternaries.solution.ts} (100%) rename src/038-unions-and-narrowing/{049-narrowing-with-nullish-coalescing.problem.ts => 050-narrowing-with-nullish-coalescing.problem.ts} (100%) rename src/038-unions-and-narrowing/{049-narrowing-with-nullish-coalescing.solution.ts => 050-narrowing-with-nullish-coalescing.solution.ts} (100%) rename src/038-unions-and-narrowing/{050-intro-to-discriminated-unions.problem.ts => 051-intro-to-discriminated-unions.problem.ts} (100%) rename src/038-unions-and-narrowing/{050-intro-to-discriminated-unions.solution.ts => 051-intro-to-discriminated-unions.solution.ts} (100%) rename src/038-unions-and-narrowing/{051-destructuring-a-discriminated-union.problem.ts => 052-destructuring-a-discriminated-union.problem.ts} (100%) rename src/038-unions-and-narrowing/{051-destructuring-a-discriminated-union.solution.ts => 052-destructuring-a-discriminated-union.solution.ts} (100%) rename src/038-unions-and-narrowing/{052-narrowing-a-discriminated-union-with-a-switch-statement.problem.ts => 053-narrowing-a-discriminated-union-with-a-switch-statement.problem.ts} (100%) rename src/038-unions-and-narrowing/{052-narrowing-a-discriminated-union-with-a-switch-statement.solution.ts => 053-narrowing-a-discriminated-union-with-a-switch-statement.solution.ts} (100%) rename src/038-unions-and-narrowing/{053-narrowing-with-switch-true.explainer.ts => 054-narrowing-with-switch-true.explainer.ts} (100%) rename src/038-unions-and-narrowing/{054-destructuring-a-discriminated-tuple.problem.ts => 055-destructuring-a-discriminated-tuple.problem.ts} (100%) rename src/038-unions-and-narrowing/{054-destructuring-a-discriminated-tuple.solution.ts => 055-destructuring-a-discriminated-tuple.solution.ts} (100%) rename src/038-unions-and-narrowing/{055-discriminated-booleans.problem.ts => 056-discriminated-booleans.problem.ts} (100%) rename src/038-unions-and-narrowing/{055-discriminated-booleans.solution.ts => 056-discriminated-booleans.solution.ts} (100%) diff --git a/src/038-unions-and-narrowing/039-combining-unions.problem.ts b/src/038-unions-and-narrowing/039-combining-unions.problem.ts new file mode 100644 index 0000000..2bcc3d6 --- /dev/null +++ b/src/038-unions-and-narrowing/039-combining-unions.problem.ts @@ -0,0 +1,21 @@ +type HttpCode = "400" | "401" | "404" | "500" | "200" | "201" | "204"; + +const handleErrorCase = (code: string) => { + // An imaginary function where we only handle the errors + + type test = Expect>; +}; + +const handleSuccessCase = (code: string) => { + // An imaginary function where we only handle the success cases + + type test = Expect>; +}; + +const handleAllCase = (code: HttpCode) => { + // An imaginary function where we handle all the cases + + type test = Expect< + Equal + >; +}; diff --git a/src/038-unions-and-narrowing/039-combining-unions.solution.ts b/src/038-unions-and-narrowing/039-combining-unions.solution.ts new file mode 100644 index 0000000..374c7f0 --- /dev/null +++ b/src/038-unions-and-narrowing/039-combining-unions.solution.ts @@ -0,0 +1,24 @@ +type SuccessCode = "200" | "201" | "204"; +type ErrorCode = "400" | "401" | "404" | "500"; + +type HttpCode = SuccessCode | ErrorCode; + +const handleErrorCase = (code: ErrorCode) => { + // An imaginary function where we only handle the errors + + type test = Expect>; +}; + +const handleSuccessCase = (code: SuccessCode) => { + // An imaginary function where we only handle the success cases + + type test = Expect>; +}; + +const handleAllCase = (code: HttpCode) => { + // An imaginary function where we handle all the cases + + type test = Expect< + Equal + >; +}; diff --git a/src/038-unions-and-narrowing/039-how-big-can-a-union-be.explainer.ts b/src/038-unions-and-narrowing/040-how-big-can-a-union-be.explainer.ts similarity index 100% rename from src/038-unions-and-narrowing/039-how-big-can-a-union-be.explainer.ts rename to src/038-unions-and-narrowing/040-how-big-can-a-union-be.explainer.ts diff --git a/src/038-unions-and-narrowing/040-literals-vs-wider-types.explainer.ts b/src/038-unions-and-narrowing/041-literals-vs-wider-types.explainer.ts similarity index 100% rename from src/038-unions-and-narrowing/040-literals-vs-wider-types.explainer.ts rename to src/038-unions-and-narrowing/041-literals-vs-wider-types.explainer.ts diff --git a/src/038-unions-and-narrowing/041-narrowing-unions-with-typeof.explainer.ts b/src/038-unions-and-narrowing/042-narrowing-unions-with-typeof.explainer.ts similarity index 100% rename from src/038-unions-and-narrowing/041-narrowing-unions-with-typeof.explainer.ts rename to src/038-unions-and-narrowing/042-narrowing-unions-with-typeof.explainer.ts diff --git a/src/038-unions-and-narrowing/042-narrowing-with-if-statements.problem.ts b/src/038-unions-and-narrowing/043-narrowing-with-if-statements.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/042-narrowing-with-if-statements.problem.ts rename to src/038-unions-and-narrowing/043-narrowing-with-if-statements.problem.ts diff --git a/src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.1.ts b/src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.1.ts similarity index 100% rename from src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.1.ts rename to src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.1.ts diff --git a/src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.2.ts b/src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.2.ts similarity index 100% rename from src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.2.ts rename to src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.2.ts diff --git a/src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.3.ts b/src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.3.ts similarity index 100% rename from src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.3.ts rename to src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.3.ts diff --git a/src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.4.ts b/src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.4.ts similarity index 100% rename from src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.4.ts rename to src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.4.ts diff --git a/src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.5.ts b/src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.5.ts similarity index 100% rename from src/038-unions-and-narrowing/042-narrowing-with-if-statements.solution.5.ts rename to src/038-unions-and-narrowing/043-narrowing-with-if-statements.solution.5.ts diff --git a/src/038-unions-and-narrowing/043-narrowing-with-boolean-wont-work.explainer.ts b/src/038-unions-and-narrowing/044-narrowing-with-boolean-wont-work.explainer.ts similarity index 100% rename from src/038-unions-and-narrowing/043-narrowing-with-boolean-wont-work.explainer.ts rename to src/038-unions-and-narrowing/044-narrowing-with-boolean-wont-work.explainer.ts diff --git a/src/038-unions-and-narrowing/044-throwing-errors-to-narrow.problem.ts b/src/038-unions-and-narrowing/045-throwing-errors-to-narrow.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/044-throwing-errors-to-narrow.problem.ts rename to src/038-unions-and-narrowing/045-throwing-errors-to-narrow.problem.ts diff --git a/src/038-unions-and-narrowing/044-throwing-errors-to-narrow.solution.ts b/src/038-unions-and-narrowing/045-throwing-errors-to-narrow.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/044-throwing-errors-to-narrow.solution.ts rename to src/038-unions-and-narrowing/045-throwing-errors-to-narrow.solution.ts diff --git a/src/038-unions-and-narrowing/045-narrowing-with-instanceof-statements.problem.ts b/src/038-unions-and-narrowing/046-narrowing-with-instanceof-statements.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/045-narrowing-with-instanceof-statements.problem.ts rename to src/038-unions-and-narrowing/046-narrowing-with-instanceof-statements.problem.ts diff --git a/src/038-unions-and-narrowing/045-narrowing-with-instanceof-statements.solution.ts b/src/038-unions-and-narrowing/046-narrowing-with-instanceof-statements.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/045-narrowing-with-instanceof-statements.solution.ts rename to src/038-unions-and-narrowing/046-narrowing-with-instanceof-statements.solution.ts diff --git a/src/038-unions-and-narrowing/046-narrowing-with-in-statements.problem.ts b/src/038-unions-and-narrowing/047-narrowing-with-in-statements.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/046-narrowing-with-in-statements.problem.ts rename to src/038-unions-and-narrowing/047-narrowing-with-in-statements.problem.ts diff --git a/src/038-unions-and-narrowing/046-narrowing-with-in-statements.solution.ts b/src/038-unions-and-narrowing/047-narrowing-with-in-statements.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/046-narrowing-with-in-statements.solution.ts rename to src/038-unions-and-narrowing/047-narrowing-with-in-statements.solution.ts diff --git a/src/038-unions-and-narrowing/047-narrowing-unknown-to-a-value.problem.ts b/src/038-unions-and-narrowing/048-narrowing-unknown-to-a-value.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/047-narrowing-unknown-to-a-value.problem.ts rename to src/038-unions-and-narrowing/048-narrowing-unknown-to-a-value.problem.ts diff --git a/src/038-unions-and-narrowing/047-narrowing-unknown-to-a-value.solution.ts b/src/038-unions-and-narrowing/048-narrowing-unknown-to-a-value.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/047-narrowing-unknown-to-a-value.solution.ts rename to src/038-unions-and-narrowing/048-narrowing-unknown-to-a-value.solution.ts diff --git a/src/038-unions-and-narrowing/048-narrowing-with-ternaries.problem.ts b/src/038-unions-and-narrowing/049-narrowing-with-ternaries.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/048-narrowing-with-ternaries.problem.ts rename to src/038-unions-and-narrowing/049-narrowing-with-ternaries.problem.ts diff --git a/src/038-unions-and-narrowing/048-narrowing-with-ternaries.solution.ts b/src/038-unions-and-narrowing/049-narrowing-with-ternaries.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/048-narrowing-with-ternaries.solution.ts rename to src/038-unions-and-narrowing/049-narrowing-with-ternaries.solution.ts diff --git a/src/038-unions-and-narrowing/049-narrowing-with-nullish-coalescing.problem.ts b/src/038-unions-and-narrowing/050-narrowing-with-nullish-coalescing.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/049-narrowing-with-nullish-coalescing.problem.ts rename to src/038-unions-and-narrowing/050-narrowing-with-nullish-coalescing.problem.ts diff --git a/src/038-unions-and-narrowing/049-narrowing-with-nullish-coalescing.solution.ts b/src/038-unions-and-narrowing/050-narrowing-with-nullish-coalescing.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/049-narrowing-with-nullish-coalescing.solution.ts rename to src/038-unions-and-narrowing/050-narrowing-with-nullish-coalescing.solution.ts diff --git a/src/038-unions-and-narrowing/050-intro-to-discriminated-unions.problem.ts b/src/038-unions-and-narrowing/051-intro-to-discriminated-unions.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/050-intro-to-discriminated-unions.problem.ts rename to src/038-unions-and-narrowing/051-intro-to-discriminated-unions.problem.ts diff --git a/src/038-unions-and-narrowing/050-intro-to-discriminated-unions.solution.ts b/src/038-unions-and-narrowing/051-intro-to-discriminated-unions.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/050-intro-to-discriminated-unions.solution.ts rename to src/038-unions-and-narrowing/051-intro-to-discriminated-unions.solution.ts diff --git a/src/038-unions-and-narrowing/051-destructuring-a-discriminated-union.problem.ts b/src/038-unions-and-narrowing/052-destructuring-a-discriminated-union.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/051-destructuring-a-discriminated-union.problem.ts rename to src/038-unions-and-narrowing/052-destructuring-a-discriminated-union.problem.ts diff --git a/src/038-unions-and-narrowing/051-destructuring-a-discriminated-union.solution.ts b/src/038-unions-and-narrowing/052-destructuring-a-discriminated-union.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/051-destructuring-a-discriminated-union.solution.ts rename to src/038-unions-and-narrowing/052-destructuring-a-discriminated-union.solution.ts diff --git a/src/038-unions-and-narrowing/052-narrowing-a-discriminated-union-with-a-switch-statement.problem.ts b/src/038-unions-and-narrowing/053-narrowing-a-discriminated-union-with-a-switch-statement.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/052-narrowing-a-discriminated-union-with-a-switch-statement.problem.ts rename to src/038-unions-and-narrowing/053-narrowing-a-discriminated-union-with-a-switch-statement.problem.ts diff --git a/src/038-unions-and-narrowing/052-narrowing-a-discriminated-union-with-a-switch-statement.solution.ts b/src/038-unions-and-narrowing/053-narrowing-a-discriminated-union-with-a-switch-statement.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/052-narrowing-a-discriminated-union-with-a-switch-statement.solution.ts rename to src/038-unions-and-narrowing/053-narrowing-a-discriminated-union-with-a-switch-statement.solution.ts diff --git a/src/038-unions-and-narrowing/053-narrowing-with-switch-true.explainer.ts b/src/038-unions-and-narrowing/054-narrowing-with-switch-true.explainer.ts similarity index 100% rename from src/038-unions-and-narrowing/053-narrowing-with-switch-true.explainer.ts rename to src/038-unions-and-narrowing/054-narrowing-with-switch-true.explainer.ts diff --git a/src/038-unions-and-narrowing/054-destructuring-a-discriminated-tuple.problem.ts b/src/038-unions-and-narrowing/055-destructuring-a-discriminated-tuple.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/054-destructuring-a-discriminated-tuple.problem.ts rename to src/038-unions-and-narrowing/055-destructuring-a-discriminated-tuple.problem.ts diff --git a/src/038-unions-and-narrowing/054-destructuring-a-discriminated-tuple.solution.ts b/src/038-unions-and-narrowing/055-destructuring-a-discriminated-tuple.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/054-destructuring-a-discriminated-tuple.solution.ts rename to src/038-unions-and-narrowing/055-destructuring-a-discriminated-tuple.solution.ts diff --git a/src/038-unions-and-narrowing/055-discriminated-booleans.problem.ts b/src/038-unions-and-narrowing/056-discriminated-booleans.problem.ts similarity index 100% rename from src/038-unions-and-narrowing/055-discriminated-booleans.problem.ts rename to src/038-unions-and-narrowing/056-discriminated-booleans.problem.ts diff --git a/src/038-unions-and-narrowing/055-discriminated-booleans.solution.ts b/src/038-unions-and-narrowing/056-discriminated-booleans.solution.ts similarity index 100% rename from src/038-unions-and-narrowing/055-discriminated-booleans.solution.ts rename to src/038-unions-and-narrowing/056-discriminated-booleans.solution.ts