Skip to content

Commit

Permalink
Added exercise on combining unions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Aug 15, 2023
1 parent 1d8ee85 commit f40f234
Show file tree
Hide file tree
Showing 35 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/038-unions-and-narrowing/039-combining-unions.problem.ts
Original file line number Diff line number Diff line change
@@ -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<Equal<typeof code, "400" | "401" | "404" | "500">>;
};

const handleSuccessCase = (code: string) => {
// An imaginary function where we only handle the success cases

type test = Expect<Equal<typeof code, "200" | "201" | "204">>;
};

const handleAllCase = (code: HttpCode) => {
// An imaginary function where we handle all the cases

type test = Expect<
Equal<typeof code, "200" | "201" | "204" | "400" | "401" | "404" | "500">
>;
};
24 changes: 24 additions & 0 deletions src/038-unions-and-narrowing/039-combining-unions.solution.ts
Original file line number Diff line number Diff line change
@@ -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<Equal<typeof code, "400" | "401" | "404" | "500">>;
};

const handleSuccessCase = (code: SuccessCode) => {
// An imaginary function where we only handle the success cases

type test = Expect<Equal<typeof code, "200" | "201" | "204">>;
};

const handleAllCase = (code: HttpCode) => {
// An imaginary function where we handle all the cases

type test = Expect<
Equal<typeof code, "200" | "201" | "204" | "400" | "401" | "404" | "500">
>;
};

0 comments on commit f40f234

Please sign in to comment.