diff --git a/package.json b/package.json index 6667aecb..964a81a0 100644 --- a/package.json +++ b/package.json @@ -60,4 +60,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..ab0978f2 100644 --- a/src/02-object-param.problem.ts +++ b/src/02-object-param.problem.ts @@ -1,6 +1,11 @@ import { expect, it } from "vitest"; -export const addTwoNumbers = (params) => { +type addTwoNumbersArgs = { + first: number; + second: number; +} + +export const addTwoNumbers = (params: addTwoNumbersArgs) => { 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..da9afc56 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: "Giuseppe", + lastName: "Vigneri", + 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..32e8b533 100644 --- a/src/07-arrays.problem.ts +++ b/src/07-arrays.problem.ts @@ -3,7 +3,8 @@ interface User { firstName: string; lastName: string; role: "admin" | "user" | "super-admin"; - posts: Post; + // posts: Post[]; + posts: Array; } interface Post { diff --git a/src/08-function-return-type-annotations.problem.ts b/src/08-function-return-type-annotations.problem.ts index af1e7217..e2873bb8 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: "Giuseppe", + lastName: "Vigneri", + role: "admin", + posts: [] + }; }; it("Should return a valid user", () => { diff --git a/src/09-promises.problem.ts b/src/09-promises.problem.ts index 09be97fc..f5debaff 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.dev/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..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..bacc8f99 100644 --- a/src/12-typeof-narrowing.problem.ts +++ b/src/12-typeof-narrowing.problem.ts @@ -1,6 +1,8 @@ import { expect, it } from "vitest"; -const coerceAmount = (amount: number | { amount: number }) => {}; +const coerceAmount = (amount: number | { amount: number }) => ( + amount instanceof Object ? amount.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..e8c3e970 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..f7b4109c 100644 --- a/src/14-extends.problem.ts +++ b/src/14-extends.problem.ts @@ -6,20 +6,22 @@ import { Equal, Expect } from "./helpers/type-utils"; * make it more DRY? */ -interface User { +interface Base { + id: string; +} + +interface User extends Base{ id: string; 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..af79e231 100644 --- a/src/15-intersection.problem.ts +++ b/src/15-intersection.problem.ts @@ -10,11 +10,15 @@ interface Post { body: string; } +interface ReturnValue extends User { + posts: Post[] +} + /** * How do we type this return statement so it's both * User AND { posts: Post[] } */ -export const getDefaultUserAndPosts = (): unknown => { +export const getDefaultUserAndPosts = (): ReturnValue => { 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..3a595cb6 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..f1901429 100644 --- a/src/17-function-types.problem.ts +++ b/src/17-function-types.problem.ts @@ -3,7 +3,10 @@ import { Equal, Expect } from "./helpers/type-utils"; /** * How do we type onFocusChange? */ -const addListener = (onFocusChange: unknown) => { + +type FocusListener = (isFocused: boolean) => void; + +const addListener = (onFocusChange: FocusListener) => { 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..b380a0d6 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: (userId: string) => Promise, ): Promise => { const userId: string = await createUser();