From 8f87299547ef0bcfff998110f6e6196e23d6ce9e Mon Sep 17 00:00:00 2001 From: Ming Date: Wed, 5 Mar 2025 11:53:49 +0800 Subject: [PATCH] finish exercise --- package.json | 2 +- src/01-number.problem.ts | 2 +- src/02-object-param.problem.ts | 2 +- src/03-optional-properties.problem.ts | 2 +- src/04-optional-params.problem.ts | 2 +- src/05-assigning-types-to-variables.problem.ts | 7 ++++++- src/06-unions.problem.ts | 2 +- src/07-arrays.problem.ts | 2 +- ...08-function-return-type-annotations.problem.ts | 15 +++++++++++++-- src/09-promises.problem.ts | 2 +- src/10-set.problem.ts | 2 +- src/11-record.problem.ts | 2 +- src/12-typeof-narrowing.problem.ts | 7 ++++++- src/13-catch-blocks.problem.ts | 4 +++- src/14-extends.problem.ts | 11 +++++------ src/15-intersection.problem.ts | 2 +- src/16-omit-and-pick.problem.ts | 2 +- src/17-function-types.problem.ts | 2 +- src/18-function-types-with-promises.problem.ts | 4 ++-- 19 files changed, 48 insertions(+), 26 deletions(-) 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 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..4b7248ce 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 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}`; } 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..13e071ba 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 55420fd0..c441e656 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..81cb5e76 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 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(); 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..d9130445 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..2069b4d1 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 246d1295..c47d9362 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 e4689052..70fea5fe 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 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..99e18ffe 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 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); }); diff --git a/src/18-function-types-with-promises.problem.ts b/src/18-function-types-with-promises.problem.ts index 086ad34f..5642baf4 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();