diff --git a/package.json b/package.json index 1263eee..af25104 100644 --- a/package.json +++ b/package.json @@ -63,4 +63,4 @@ "express": "^4.18.1", "zod": "^3.17.10" } -} +} \ No newline at end of file diff --git a/src/01-number.problem.ts b/src/01-number.problem.ts index 0f6286e..5f21a88 100644 --- a/src/01-number.problem.ts +++ b/src/01-number.problem.ts @@ -1,6 +1,6 @@ import { expect, it } from "vitest"; -export const addTwoNumbers = (a, b) => { +export const addTwoNumbers = (a: number, b: number) => { return a + b; }; diff --git a/src/02-object-param.problem.ts b/src/02-object-param.problem.ts index 8c33176..4b7248c 100644 --- a/src/02-object-param.problem.ts +++ b/src/02-object-param.problem.ts @@ -1,6 +1,6 @@ import { expect, it } from "vitest"; -export const addTwoNumbers = (params) => { +export const addTwoNumbers = (params: { first: number; second: number }) => { return params.first + params.second; }; diff --git a/src/03-optional-properties.problem.ts b/src/03-optional-properties.problem.ts index 9ee58fc..a35619d 100644 --- a/src/03-optional-properties.problem.ts +++ b/src/03-optional-properties.problem.ts @@ -1,6 +1,6 @@ import { expect, it } from "vitest"; -export const getName = (params: { first: string; last: string }) => { +export const getName = (params: { first: string; last?: string }) => { if (params.last) { return `${params.first} ${params.last}`; } diff --git a/src/04-optional-params.problem.ts b/src/04-optional-params.problem.ts index 023bb99..3c12b9f 100644 --- a/src/04-optional-params.problem.ts +++ b/src/04-optional-params.problem.ts @@ -1,6 +1,6 @@ import { expect, it } from "vitest"; -export const getName = (first: string, last: string) => { +export const getName = (first: string, last?: string) => { if (last) { return `${first} ${last}`; } diff --git a/src/05-assigning-types-to-variables.problem.ts b/src/05-assigning-types-to-variables.problem.ts index 50de898..13e071b 100644 --- a/src/05-assigning-types-to-variables.problem.ts +++ b/src/05-assigning-types-to-variables.problem.ts @@ -11,7 +11,12 @@ interface User { * How do we ensure that defaultUser is of type User * at THIS LINE - not further down in the code? */ -const defaultUser = {}; +const defaultUser: User = { + id: 1, + firstName: "Matt", + lastName: "Pocock", + isAdmin: false, +}; const getUserId = (user: User) => { return user.id; diff --git a/src/06-unions.problem.ts b/src/06-unions.problem.ts index 55420fd..c441e65 100644 --- a/src/06-unions.problem.ts +++ b/src/06-unions.problem.ts @@ -8,7 +8,7 @@ interface User { * - 'user' * - 'super-admin' */ - role: string; + role: "admin" | "user" | "super-admin"; } export const defaultUser: User = { diff --git a/src/07-arrays.problem.ts b/src/07-arrays.problem.ts index c18909c..dda09a2 100644 --- a/src/07-arrays.problem.ts +++ b/src/07-arrays.problem.ts @@ -3,7 +3,7 @@ interface User { firstName: string; lastName: string; role: "admin" | "user" | "super-admin"; - posts: Post; + posts: Post[]; } interface Post { diff --git a/src/08-function-return-type-annotations.problem.ts b/src/08-function-return-type-annotations.problem.ts index af1e721..81cb5e7 100644 --- a/src/08-function-return-type-annotations.problem.ts +++ b/src/08-function-return-type-annotations.problem.ts @@ -17,8 +17,19 @@ interface Post { * How do we ensure that makeUser ALWAYS * returns a user? */ -const makeUser = () => { - return {}; +const makeUser = (): User => { + return { + id: 1, + firstName: "John", + lastName: "Doe", + role: "user", + posts: [ + { + id: 1, + title: "How I eat so much cheese", + }, + ], + }; }; it("Should return a valid user", () => { diff --git a/src/09-promises.problem.ts b/src/09-promises.problem.ts index cc5781b..851d1e0 100644 --- a/src/09-promises.problem.ts +++ b/src/09-promises.problem.ts @@ -9,7 +9,7 @@ interface LukeSkywalker { gender: string; } -export const fetchLukeSkywalker = async (): LukeSkywalker => { +export const fetchLukeSkywalker = async (): Promise => { const data = await fetch("https://swapi.py4e.com/api/people/1").then( (res) => { return res.json(); diff --git a/src/10-set.problem.ts b/src/10-set.problem.ts index 0f318a4..0bd251c 100644 --- a/src/10-set.problem.ts +++ b/src/10-set.problem.ts @@ -1,7 +1,7 @@ import { expect, it } from "vitest"; import { Equal, Expect } from "./helpers/type-utils"; -const guitarists = new Set(); +const guitarists = new Set(); guitarists.add("Jimi Hendrix"); guitarists.add("Eric Clapton"); diff --git a/src/11-record.problem.ts b/src/11-record.problem.ts index 1cc7445..d913044 100644 --- a/src/11-record.problem.ts +++ b/src/11-record.problem.ts @@ -1,7 +1,7 @@ import { expect, it } from "vitest"; const createCache = () => { - const cache = {}; + const cache:Record = { }; const add = (id: string, value: string) => { cache[id] = value; diff --git a/src/12-typeof-narrowing.problem.ts b/src/12-typeof-narrowing.problem.ts index 7811011..2069b4d 100644 --- a/src/12-typeof-narrowing.problem.ts +++ b/src/12-typeof-narrowing.problem.ts @@ -1,6 +1,11 @@ import { expect, it } from "vitest"; -const coerceAmount = (amount: number | { amount: number }) => {}; +const coerceAmount = (amount: number | { amount: number }) => { + if (typeof amount === "number") { + return amount; + } + return amount.amount; +}; it("Should return the amount when passed an object", () => { expect(coerceAmount({ amount: 20 })).toEqual(20); diff --git a/src/13-catch-blocks.problem.ts b/src/13-catch-blocks.problem.ts index 246d129..c47d936 100644 --- a/src/13-catch-blocks.problem.ts +++ b/src/13-catch-blocks.problem.ts @@ -6,7 +6,9 @@ const tryCatchDemo = (state: "fail" | "succeed") => { throw new Error("Failure!"); } } catch (e) { - return e.message; + if (e instanceof Error) { + return e.message; + } } }; diff --git a/src/14-extends.problem.ts b/src/14-extends.problem.ts index e468905..70fea5f 100644 --- a/src/14-extends.problem.ts +++ b/src/14-extends.problem.ts @@ -5,21 +5,20 @@ import { Equal, Expect } from "./helpers/type-utils"; * interfaces. Can you find a way to refactor this to * make it more DRY? */ - -interface User { +interface Base { id: string; +} +interface User extends Base { firstName: string; lastName: string; } -interface Post { - id: string; +interface Post extends Base { title: string; body: string; } -interface Comment { - id: string; +interface Comment extends Base { comment: string; } diff --git a/src/15-intersection.problem.ts b/src/15-intersection.problem.ts index a6e5bc7..9569caa 100644 --- a/src/15-intersection.problem.ts +++ b/src/15-intersection.problem.ts @@ -14,7 +14,7 @@ interface Post { * How do we type this return statement so it's both * User AND { posts: Post[] } */ -export const getDefaultUserAndPosts = (): unknown => { +export const getDefaultUserAndPosts = (): User & { posts: Post[] } => { return { id: "1", firstName: "Matt", diff --git a/src/16-omit-and-pick.problem.ts b/src/16-omit-and-pick.problem.ts index 8d75c8b..99e18ff 100644 --- a/src/16-omit-and-pick.problem.ts +++ b/src/16-omit-and-pick.problem.ts @@ -11,6 +11,6 @@ interface User { * firstName and lastName properties of User? */ -type MyType = unknown; +type MyType = Pick; type tests = [Expect>]; diff --git a/src/17-function-types.problem.ts b/src/17-function-types.problem.ts index a228d9e..12e6a41 100644 --- a/src/17-function-types.problem.ts +++ b/src/17-function-types.problem.ts @@ -3,7 +3,7 @@ import { Equal, Expect } from "./helpers/type-utils"; /** * How do we type onFocusChange? */ -const addListener = (onFocusChange: unknown) => { +const addListener = (onFocusChange: (isFocused: boolean) => void) => { window.addEventListener("focus", () => { onFocusChange(true); }); diff --git a/src/18-function-types-with-promises.problem.ts b/src/18-function-types-with-promises.problem.ts index 086ad34..5642baf 100644 --- a/src/18-function-types-with-promises.problem.ts +++ b/src/18-function-types-with-promises.problem.ts @@ -7,8 +7,8 @@ interface User { } const createThenGetUser = async ( - createUser: unknown, - getUser: unknown, + createUser: () => Promise, + getUser: (id: string) => Promise, ): Promise => { const userId: string = await createUser();