-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed 221 exercise and improved type predicates section
- Loading branch information
1 parent
c5f2ca1
commit f6d4961
Showing
3 changed files
with
150 additions
and
23 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
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,16 +1,68 @@ | ||
import { Equal, Expect } from "@total-typescript/helpers"; | ||
import { expect, it } from "vitest"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
const isString = (input: unknown) => { | ||
return typeof input === "string"; | ||
const hasDataAndId = (value: unknown) => { | ||
return ( | ||
typeof value === "object" && | ||
value !== null && | ||
"data" in value && | ||
typeof value.data === "object" && | ||
value.data !== null && | ||
"id" in value.data && | ||
typeof value.data.id === "string" | ||
); | ||
}; | ||
|
||
it("Should be able to be passed to .filter and work", () => { | ||
const mixedArray = [1, "hello", [], {}]; | ||
const parseValue = (value: unknown) => { | ||
if (hasDataAndId(value)) { | ||
return value.data.id; | ||
} | ||
|
||
const stringsOnly = mixedArray.filter(isString); | ||
throw new Error("Parsing error!"); | ||
}; | ||
|
||
const parseValueAgain = (value: unknown) => { | ||
if (hasDataAndId(value)) { | ||
return value.data.id; | ||
} | ||
|
||
throw new Error("Parsing error!"); | ||
}; | ||
|
||
describe("parseValue", () => { | ||
it("Should handle a { data: { id: string } }", () => { | ||
const result = parseValue({ | ||
data: { | ||
id: "123", | ||
}, | ||
}); | ||
|
||
type test = Expect<Equal<typeof result, string>>; | ||
|
||
expect(result).toBe("123"); | ||
}); | ||
|
||
it("Should error when anything else is passed in", () => { | ||
expect(() => parseValue("123")).toThrow("Parsing error!"); | ||
expect(() => parseValue(123)).toThrow("Parsing error!"); | ||
}); | ||
}); | ||
|
||
describe("parseValueAgain", () => { | ||
it("Should handle a { data: { id: string } }", () => { | ||
const result = parseValueAgain({ | ||
data: { | ||
id: "123", | ||
}, | ||
}); | ||
|
||
type test = Expect<Equal<typeof result, string>>; | ||
|
||
type test1 = Expect<Equal<typeof stringsOnly, string[]>>; | ||
expect(result).toBe("123"); | ||
}); | ||
|
||
expect(stringsOnly).toEqual(["hello"]); | ||
it("Should error when anything else is passed in", () => { | ||
expect(() => parseValueAgain("123")).toThrow("Parsing error!"); | ||
expect(() => parseValueAgain(123)).toThrow("Parsing error!"); | ||
}); | ||
}); |
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,16 +1,74 @@ | ||
import { Equal, Expect } from "@total-typescript/helpers"; | ||
import { expect, it } from "vitest"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
const isString = (input: unknown): input is string => { | ||
return typeof input === "string"; | ||
const hasDataAndId = ( | ||
value: unknown, | ||
): value is { | ||
data: { | ||
id: string; | ||
}; | ||
} => { | ||
return ( | ||
typeof value === "object" && | ||
value !== null && | ||
"data" in value && | ||
typeof value.data === "object" && | ||
value.data !== null && | ||
"id" in value.data && | ||
typeof value.data.id === "string" | ||
); | ||
}; | ||
|
||
it("Should be able to be passed to .filter and work", () => { | ||
const mixedArray = [1, "hello", [], {}]; | ||
const parseValue = (value: unknown) => { | ||
if (hasDataAndId(value)) { | ||
return value.data.id; | ||
} | ||
|
||
const stringsOnly = mixedArray.filter(isString); | ||
throw new Error("Parsing error!"); | ||
}; | ||
|
||
const parseValueAgain = (value: unknown) => { | ||
if (hasDataAndId(value)) { | ||
return value.data.id; | ||
} | ||
|
||
throw new Error("Parsing error!"); | ||
}; | ||
|
||
describe("parseValue", () => { | ||
it("Should handle a { data: { id: string } }", () => { | ||
const result = parseValue({ | ||
data: { | ||
id: "123", | ||
}, | ||
}); | ||
|
||
type test = Expect<Equal<typeof result, string>>; | ||
|
||
expect(result).toBe("123"); | ||
}); | ||
|
||
it("Should error when anything else is passed in", () => { | ||
expect(() => parseValue("123")).toThrow("Parsing error!"); | ||
expect(() => parseValue(123)).toThrow("Parsing error!"); | ||
}); | ||
}); | ||
|
||
describe("parseValueAgain", () => { | ||
it("Should handle a { data: { id: string } }", () => { | ||
const result = parseValueAgain({ | ||
data: { | ||
id: "123", | ||
}, | ||
}); | ||
|
||
type test = Expect<Equal<typeof result, string>>; | ||
|
||
type test1 = Expect<Equal<typeof stringsOnly, string[]>>; | ||
expect(result).toBe("123"); | ||
}); | ||
|
||
expect(stringsOnly).toEqual(["hello"]); | ||
it("Should error when anything else is passed in", () => { | ||
expect(() => parseValueAgain("123")).toThrow("Parsing error!"); | ||
expect(() => parseValueAgain(123)).toThrow("Parsing error!"); | ||
}); | ||
}); |