From 022c356966058eaf2a212da38b2d976cd02b6e53 Mon Sep 17 00:00:00 2001 From: kylenoad Date: Thu, 15 May 2025 12:36:00 +0100 Subject: [PATCH 1/5] installed dependancies --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1263eee7..af251048 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 From ae18f424f2e590883c04a7adb8630e4f454a8a81 Mon Sep 17 00:00:00 2001 From: kylenoad Date: Fri, 16 May 2025 10:47:59 +0100 Subject: [PATCH 2/5] solution 1 and 2 --- src/01-number.problem.ts | 2 +- src/02-object-param.problem.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/01-number.problem.ts b/src/01-number.problem.ts index 0f6286e0..5f21a88e 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 8c331765..762f0c14 100644 --- a/src/02-object-param.problem.ts +++ b/src/02-object-param.problem.ts @@ -1,6 +1,8 @@ import { expect, it } from "vitest"; -export const addTwoNumbers = (params) => { +type NewType = { first: number; second: number} + +export const addTwoNumbers = (params: NewType) => { return params.first + params.second; }; From f9fd9ac8c45d1925efb4beed09831e923c0fcd9a Mon Sep 17 00:00:00 2001 From: kylenoad Date: Fri, 16 May 2025 12:07:12 +0100 Subject: [PATCH 3/5] solution number 3 --- src/03-optional-properties.problem.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/03-optional-properties.problem.ts b/src/03-optional-properties.problem.ts index 9ee58fcb..a35619d0 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}`; } From a22f9c5472bf5f39e590d0f2329225fd97c19cfa Mon Sep 17 00:00:00 2001 From: kylenoad Date: Fri, 16 May 2025 12:51:48 +0100 Subject: [PATCH 4/5] completed solutions to 9 --- src/04-optional-params.problem.ts | 2 +- src/05-assigning-types-to-variables.problem.ts | 8 +++++++- src/06-unions.problem.ts | 2 +- src/07-arrays.problem.ts | 2 +- src/08-function-return-type-annotations.problem.ts | 10 ++++++++-- src/09-promises.problem.ts | 2 +- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/04-optional-params.problem.ts b/src/04-optional-params.problem.ts index 023bb997..3c12b9f8 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 50de8989..02fc329f 100644 --- a/src/05-assigning-types-to-variables.problem.ts +++ b/src/05-assigning-types-to-variables.problem.ts @@ -11,7 +11,13 @@ 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: "Kyle", + lastName: "Noad", + isAdmin: true + +}; const getUserId = (user: User) => { return user.id; diff --git a/src/06-unions.problem.ts b/src/06-unions.problem.ts index 55420fd0..98d850e7 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 c18909ce..dda09a25 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 af1e7217..4089cd9a 100644 --- a/src/08-function-return-type-annotations.problem.ts +++ b/src/08-function-return-type-annotations.problem.ts @@ -17,8 +17,14 @@ interface Post { * How do we ensure that makeUser ALWAYS * returns a user? */ -const makeUser = () => { - return {}; +const makeUser = (): User => { + return { + id: 1, + firstName: "Kyle", + lastName: "Noad", + role: "super-admin", + posts: [{id: 1, title: "hello"}] + }; }; it("Should return a valid user", () => { diff --git a/src/09-promises.problem.ts b/src/09-promises.problem.ts index cc5781bb..851d1e0a 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(); From 94875b97cfe6c72f3fe8f802fa06d624f1105837 Mon Sep 17 00:00:00 2001 From: kylenoad Date: Fri, 16 May 2025 15:17:09 +0100 Subject: [PATCH 5/5] solved up to q18 --- src/10-set.problem.ts | 2 +- src/11-record.problem.ts | 2 +- src/12-typeof-narrowing.problem.ts | 8 +++++++- src/13-catch-blocks.problem.ts | 2 +- src/14-extends.problem.ts | 13 +++++++------ src/15-intersection.problem.ts | 2 +- src/16-omit-and-pick.problem.ts | 2 +- src/17-function-types.problem.ts | 2 +- 8 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/10-set.problem.ts b/src/10-set.problem.ts index 0f318a4c..0bd251cc 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 1cc74453..5f0b6a33 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 7811011a..732a7415 100644 --- a/src/12-typeof-narrowing.problem.ts +++ b/src/12-typeof-narrowing.problem.ts @@ -1,6 +1,12 @@ import { expect, it } from "vitest"; -const coerceAmount = (amount: number | { amount: number }) => {}; +const coerceAmount = (amount: number | { amount: number }) => { + if(typeof amount === "number"){ + return amount + }else{ + 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 246d1295..0a1723e6 100644 --- a/src/13-catch-blocks.problem.ts +++ b/src/13-catch-blocks.problem.ts @@ -5,7 +5,7 @@ const tryCatchDemo = (state: "fail" | "succeed") => { if (state === "fail") { throw new Error("Failure!"); } - } catch (e) { + } catch (e: any) { return e.message; } }; diff --git a/src/14-extends.problem.ts b/src/14-extends.problem.ts index e4689052..def267de 100644 --- a/src/14-extends.problem.ts +++ b/src/14-extends.problem.ts @@ -6,20 +6,21 @@ import { Equal, Expect } from "./helpers/type-utils"; * make it more DRY? */ -interface User { - id: string; +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 a6e5bc7d..9569caa0 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 8d75c8b9..087fadda 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 = Omit; type tests = [Expect>]; diff --git a/src/17-function-types.problem.ts b/src/17-function-types.problem.ts index a228d9e7..12e6a417 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); });