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..762f0c1 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; }; 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..02fc329 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 55420fd..98d850e 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..4089cd9 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 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..5f0b6a3 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..732a741 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 246d129..0a1723e 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 e468905..def267d 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 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..087fadd 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 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); });