-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23fe0aa
commit c5f2ca1
Showing
5 changed files
with
71 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
src/018-unions-and-narrowing/066.5-reusable-type-guards.problem.ts
This file was deleted.
Oops, something went wrong.
60 changes: 0 additions & 60 deletions
60
src/018-unions-and-narrowing/066.5-reusable-type-guards.solution.ts
This file was deleted.
Oops, something went wrong.
62 changes: 19 additions & 43 deletions
62
src/018-unions-and-narrowing/072.5-reusable-type-guards.problem.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,48 @@ | ||
import { Equal, Expect } from "@total-typescript/helpers"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
const parseValue = (value: unknown) => { | ||
if ( | ||
typeof value === "object" && | ||
value !== null && | ||
"data" in value && | ||
typeof value.data === "object" && | ||
value.data !== null && | ||
"id" in value.data && | ||
typeof value.data.id === "string" | ||
) { | ||
return value.data.id; | ||
const joinNames = (value: unknown) => { | ||
if (Array.isArray(value) && value.every((item) => typeof item === "string")) { | ||
return value.join(" "); | ||
} | ||
|
||
throw new Error("Parsing error!"); | ||
}; | ||
|
||
const parseValueAgain = (value: unknown) => { | ||
if ( | ||
typeof value === "object" && | ||
value !== null && | ||
"data" in value && | ||
typeof value.data === "object" && | ||
value.data !== null && | ||
"id" in value.data && | ||
typeof value.data.id === "string" | ||
) { | ||
return value.data.id; | ||
const createSections = (value: unknown) => { | ||
if (Array.isArray(value) && value.every((item) => typeof item === "string")) { | ||
return value.map((item) => `Section: ${item}`); | ||
} | ||
|
||
throw new Error("Parsing error!"); | ||
}; | ||
|
||
describe("parseValue", () => { | ||
it("Should handle a { data: { id: string } }", () => { | ||
const result = parseValue({ | ||
data: { | ||
id: "123", | ||
}, | ||
}); | ||
describe("joinNames", () => { | ||
it("Should handle an array of strings", () => { | ||
const result = joinNames(["John", "Doe"]); | ||
|
||
type test = Expect<Equal<typeof result, string>>; | ||
|
||
expect(result).toBe("123"); | ||
expect(result).toBe("John Doe"); | ||
}); | ||
|
||
it("Should error when anything else is passed in", () => { | ||
expect(() => parseValue("123")).toThrow("Parsing error!"); | ||
expect(() => parseValue(123)).toThrow("Parsing error!"); | ||
expect(() => joinNames("John")).toThrow("Parsing error!"); | ||
expect(() => joinNames(123)).toThrow("Parsing error!"); | ||
}); | ||
}); | ||
|
||
describe("parseValueAgain", () => { | ||
it("Should handle a { data: { id: string } }", () => { | ||
const result = parseValueAgain({ | ||
data: { | ||
id: "123", | ||
}, | ||
}); | ||
describe("createSections", () => { | ||
it("Should handle an array of strings", () => { | ||
const result = createSections(["John", "Doe"]); | ||
|
||
type test = Expect<Equal<typeof result, string>>; | ||
type test = Expect<Equal<typeof result, string[]>>; | ||
|
||
expect(result).toBe("123"); | ||
expect(result).toEqual(["Section: John", "Section: Doe"]); | ||
}); | ||
|
||
it("Should error when anything else is passed in", () => { | ||
expect(() => parseValueAgain("123")).toThrow("Parsing error!"); | ||
expect(() => parseValueAgain(123)).toThrow("Parsing error!"); | ||
expect(() => createSections("John")).toThrow("Parsing error!"); | ||
expect(() => createSections(123)).toThrow("Parsing error!"); | ||
}); | ||
}); |
Oops, something went wrong.