From 3250311f3606f10896b300e184384b1b95538dfa Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 01:59:13 +0100 Subject: [PATCH 01/19] :rocket: initial setup --- 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 180eab515b5eb95771c326849381283497206548 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 01:59:55 +0100 Subject: [PATCH 02/19] complete 01-number problem --- src/01-number.problem.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/01-number.problem.ts b/src/01-number.problem.ts index 0f6286e0..fe8ffcfe 100644 --- a/src/01-number.problem.ts +++ b/src/01-number.problem.ts @@ -1,10 +1,10 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; -export const addTwoNumbers = (a, b) => { - return a + b; +export const addTwoNumbers = (a: number, b: number) => { + return a + b; }; -it("Should add the two numbers together", () => { - expect(addTwoNumbers(2, 4)).toEqual(6); - expect(addTwoNumbers(10, 10)).toEqual(20); +it('Should add the two numbers together', () => { + expect(addTwoNumbers(2, 4)).toEqual(6); + expect(addTwoNumbers(10, 10)).toEqual(20); }); From 7d186ff971217aa38d4f326f3df64882a31b6d4c Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:03:57 +0100 Subject: [PATCH 03/19] :sparkles: complete 02-object-param problem --- src/02-object-param.problem.ts | 37 +++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/02-object-param.problem.ts b/src/02-object-param.problem.ts index 8c331765..682c529d 100644 --- a/src/02-object-param.problem.ts +++ b/src/02-object-param.problem.ts @@ -1,21 +1,26 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; -export const addTwoNumbers = (params) => { - return params.first + params.second; +type AddTwoNumbersArgs = { + first: number; + second: number; }; -it("Should add the two numbers together", () => { - expect( - addTwoNumbers({ - first: 2, - second: 4, - }), - ).toEqual(6); +export const addTwoNumbers = (params: AddTwoNumbersArgs) => { + return params.first + params.second; +}; + +it('Should add the two numbers together', () => { + expect( + addTwoNumbers({ + first: 2, + second: 4, + }) + ).toEqual(6); - expect( - addTwoNumbers({ - first: 10, - second: 20, - }), - ).toEqual(30); + expect( + addTwoNumbers({ + first: 10, + second: 20, + }) + ).toEqual(30); }); From f0147488eb330146c66eec60b28fda4f63d74114 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:05:21 +0100 Subject: [PATCH 04/19] :sparkles: complete 03-optional-properties problem --- src/03-optional-properties.problem.ts | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/03-optional-properties.problem.ts b/src/03-optional-properties.problem.ts index 9ee58fcb..b6760ab8 100644 --- a/src/03-optional-properties.problem.ts +++ b/src/03-optional-properties.problem.ts @@ -1,25 +1,25 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; -export const getName = (params: { first: string; last: string }) => { - if (params.last) { - return `${params.first} ${params.last}`; - } - return params.first; +export const getName = (params: { first: string; last?: string }) => { + if (params.last) { + return `${params.first} ${params.last}`; + } + return params.first; }; -it("Should work with just the first name", () => { - const name = getName({ - first: "Matt", - }); +it('Should work with just the first name', () => { + const name = getName({ + first: 'Matt', + }); - expect(name).toEqual("Matt"); + expect(name).toEqual('Matt'); }); -it("Should work with the first and last name", () => { - const name = getName({ - first: "Matt", - last: "Pocock", - }); +it('Should work with the first and last name', () => { + const name = getName({ + first: 'Matt', + last: 'Pocock', + }); - expect(name).toEqual("Matt Pocock"); + expect(name).toEqual('Matt Pocock'); }); From b9f6798e0aba53d01da51ccf1a8866e04005a412 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:07:31 +0100 Subject: [PATCH 05/19] :sparkles: complete 04-optional-params problem --- src/04-optional-params.problem.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/04-optional-params.problem.ts b/src/04-optional-params.problem.ts index 023bb997..4913a746 100644 --- a/src/04-optional-params.problem.ts +++ b/src/04-optional-params.problem.ts @@ -1,20 +1,20 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; -export const getName = (first: string, last: string) => { - if (last) { - return `${first} ${last}`; - } - return first; +export const getName = (first: string, last?: string) => { + if (last) { + return `${first} ${last}`; + } + return first; }; -it("Should work with just the first name", () => { - const name = getName("Matt"); +it('Should work with just the first name', () => { + const name = getName('Matt'); - expect(name).toEqual("Matt"); + expect(name).toEqual('Matt'); }); -it("Should work with the first and last name", () => { - const name = getName("Matt", "Pocock"); +it('Should work with the first and last name', () => { + const name = getName('Matt', 'Pocock'); - expect(name).toEqual("Matt Pocock"); + expect(name).toEqual('Matt Pocock'); }); From 372a39404a9109c1e60ad5035fdc0eb199295336 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:10:15 +0100 Subject: [PATCH 06/19] :sparkles: complete 05-assigning-types-to-variables problem --- src/05-assigning-types-to-variables.problem.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/05-assigning-types-to-variables.problem.ts b/src/05-assigning-types-to-variables.problem.ts index 50de8989..7718eb9a 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: 'Uchenna', + lastName: 'Egbo', + isAdmin: true +}; const getUserId = (user: User) => { return user.id; From 63b351046deba0886243ca6bc09c2641e8ccd24d Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:14:11 +0100 Subject: [PATCH 07/19] :sparkles: complete 06-unions problem --- ...05-assigning-types-to-variables.problem.ts | 30 ++++++++----------- src/06-unions.problem.ts | 25 +++++++--------- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/05-assigning-types-to-variables.problem.ts b/src/05-assigning-types-to-variables.problem.ts index 7718eb9a..93baca82 100644 --- a/src/05-assigning-types-to-variables.problem.ts +++ b/src/05-assigning-types-to-variables.problem.ts @@ -1,27 +1,23 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; interface User { - id: number; - firstName: string; - lastName: string; - isAdmin: boolean; + id: number; + firstName: string; + lastName: string; + isAdmin: boolean; } -/** - * How do we ensure that defaultUser is of type User - * at THIS LINE - not further down in the code? - */ -const defaultUser : User = { - id: 1, - firstName: 'Uchenna', - lastName: 'Egbo', - isAdmin: true +const defaultUser: User = { + id: 1, + firstName: 'Uchenna', + lastName: 'Egbo', + isAdmin: true, }; const getUserId = (user: User) => { - return user.id; + return user.id; }; -it("Should get the user id", () => { - expect(getUserId(defaultUser)).toEqual(1); +it('Should get the user id', () => { + expect(getUserId(defaultUser)).toEqual(1); }); diff --git a/src/06-unions.problem.ts b/src/06-unions.problem.ts index 55420fd0..928dcf6f 100644 --- a/src/06-unions.problem.ts +++ b/src/06-unions.problem.ts @@ -1,20 +1,15 @@ +type Role = 'admin' | 'user' | 'super-admin'; + interface User { - id: number; - firstName: string; - lastName: string; - /** - * How do we ensure that role is only one of: - * - 'admin' - * - 'user' - * - 'super-admin' - */ - role: string; + id: number; + firstName: string; + lastName: string; + role: Role; } export const defaultUser: User = { - id: 1, - firstName: "Matt", - lastName: "Pocock", - // @ts-expect-error - role: "I_SHOULD_NOT_BE_ALLOWED", + id: 1, + firstName: 'Uchenna', + lastName: 'Egbo', + role: 'super-admin', }; From 48843f47079e69948691baf92d8ba3f2a7497722 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:15:26 +0100 Subject: [PATCH 08/19] :sparkles: complete 07-arrays problem --- src/07-arrays.problem.ts | 42 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/07-arrays.problem.ts b/src/07-arrays.problem.ts index c18909ce..4f014686 100644 --- a/src/07-arrays.problem.ts +++ b/src/07-arrays.problem.ts @@ -1,29 +1,29 @@ interface User { - id: number; - firstName: string; - lastName: string; - role: "admin" | "user" | "super-admin"; - posts: Post; + id: number; + firstName: string; + lastName: string; + role: 'admin' | 'user' | 'super-admin'; + posts: Post[]; } interface Post { - id: number; - title: string; + id: number; + title: string; } export const defaultUser: User = { - id: 1, - firstName: "Matt", - lastName: "Pocock", - role: "admin", - posts: [ - { - id: 1, - title: "How I eat so much cheese", - }, - { - id: 2, - title: "Why I don't eat more vegetables", - }, - ], + id: 1, + firstName: 'Matt', + lastName: 'Pocock', + role: 'admin', + posts: [ + { + id: 1, + title: 'How I eat so much cheese', + }, + { + id: 2, + title: "Why I don't eat more vegetables", + }, + ], }; From f8a42e085b6c37ab9f11d81946af66c8f51de8c1 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:19:54 +0100 Subject: [PATCH 09/19] :sparkles: complete 08-function-return-type-annotations problem --- ...unction-return-type-annotations.problem.ts | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/08-function-return-type-annotations.problem.ts b/src/08-function-return-type-annotations.problem.ts index af1e7217..e0baa0de 100644 --- a/src/08-function-return-type-annotations.problem.ts +++ b/src/08-function-return-type-annotations.problem.ts @@ -1,34 +1,41 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; interface User { - id: number; - firstName: string; - lastName: string; - role: "admin" | "user" | "super-admin"; - posts: Array; + id: number; + firstName: string; + lastName: string; + role: 'admin' | 'user' | 'super-admin'; + posts: Array; } interface Post { - id: number; - title: string; + id: number; + title: string; } -/** - * How do we ensure that makeUser ALWAYS - * returns a user? - */ -const makeUser = () => { - return {}; +const makeUser = (): User => { + return { + id: 1, + firstName: 'Uchenna', + lastName: 'Egbo', + role: 'super-admin', + posts: [ + { + id: 1, + title: "Beginner's Typescript", + }, + ], + }; }; -it("Should return a valid user", () => { - const user = makeUser(); +it('Should return a valid user', () => { + const user = makeUser(); - expect(user.id).toBeTypeOf("number"); - expect(user.firstName).toBeTypeOf("string"); - expect(user.lastName).toBeTypeOf("string"); - expect(user.role).to.be.oneOf(["super-admin", "admin", "user"]); + expect(user.id).toBeTypeOf('number'); + expect(user.firstName).toBeTypeOf('string'); + expect(user.lastName).toBeTypeOf('string'); + expect(user.role).to.be.oneOf(['super-admin', 'admin', 'user']); - expect(user.posts[0].id).toBeTypeOf("number"); - expect(user.posts[0].title).toBeTypeOf("string"); + expect(user.posts[0].id).toBeTypeOf('number'); + expect(user.posts[0].title).toBeTypeOf('string'); }); From 67f9aefa6d8654d44c642546094005a503c2f96f Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:24:02 +0100 Subject: [PATCH 10/19] :sparkles: complete 09-promises problem --- src/09-promises.problem.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/09-promises.problem.ts b/src/09-promises.problem.ts index 09be97fc..5af12a8c 100644 --- a/src/09-promises.problem.ts +++ b/src/09-promises.problem.ts @@ -1,18 +1,18 @@ interface LukeSkywalker { - name: string; - height: string; - mass: string; - hair_color: string; - skin_color: string; - eye_color: string; - birth_year: string; - gender: string; + name: string; + height: string; + mass: string; + hair_color: string; + skin_color: string; + eye_color: string; + birth_year: string; + gender: string; } -export const fetchLukeSkywalker = async (): LukeSkywalker => { - const data = await fetch("https://swapi.dev/api/people/1").then((res) => { - return res.json(); - }); +export const fetchLukeSkywalker = async () => { + const data = await fetch('https://swapi.dev/api/people/1').then((res) => { + return res.json(); + }); - return data; + return data as LukeSkywalker; }; From a77c4789fcff6d107cbb06b25c52ee597c609d8a Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 02:24:50 +0100 Subject: [PATCH 11/19] :sparkles: complete 10-set problem --- src/10-set.problem.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/10-set.problem.ts b/src/10-set.problem.ts index 0f318a4c..ba3e000d 100644 --- a/src/10-set.problem.ts +++ b/src/10-set.problem.ts @@ -1,23 +1,22 @@ -import { expect, it } from "vitest"; -import { Equal, Expect } from "./helpers/type-utils"; +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"); +guitarists.add('Jimi Hendrix'); +guitarists.add('Eric Clapton'); -it("Should contain Jimi and Eric", () => { - expect(guitarists.has("Jimi Hendrix")).toEqual(true); - expect(guitarists.has("Eric Clapton")).toEqual(true); +it('Should contain Jimi and Eric', () => { + expect(guitarists.has('Jimi Hendrix')).toEqual(true); + expect(guitarists.has('Eric Clapton')).toEqual(true); }); -it("Should give a type error when you try to pass a non-string", () => { - // @ts-expect-error - guitarists.add(2); +it('Should give a type error when you try to pass a non-string', () => { + guitarists.add('2'); }); -it("Should be typed as an array of strings", () => { - const guitaristsAsArray = Array.from(guitarists); +it('Should be typed as an array of strings', () => { + const guitaristsAsArray = Array.from(guitarists); - type tests = [Expect>]; + type tests = [Expect>]; }); From b2bae8a027e1d7fcf78f39d49541b6b795905468 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 11:57:20 +0100 Subject: [PATCH 12/19] :sparkles: complete 11-record problem --- src/11-record.problem.ts | 48 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/11-record.problem.ts b/src/11-record.problem.ts index 1cc74453..c23872d0 100644 --- a/src/11-record.problem.ts +++ b/src/11-record.problem.ts @@ -1,36 +1,40 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; + +interface ICache { + [index: string]: string; +} const createCache = () => { - const cache = {}; + const cache: ICache = {}; - const add = (id: string, value: string) => { - cache[id] = value; - }; + const add = (id: string, value: string) => { + cache[id] = value; + }; - const remove = (id: string) => { - delete cache[id]; - }; + const remove = (id: string) => { + delete cache[id]; + }; - return { - cache, - add, - remove, - }; + return { + cache, + add, + remove, + }; }; -it("Should add values to the cache", () => { - const cache = createCache(); +it('Should add values to the cache', () => { + const cache = createCache(); - cache.add("123", "Matt"); + cache.add('123', 'Matt'); - expect(cache.cache["123"]).toEqual("Matt"); + expect(cache.cache['123']).toEqual('Matt'); }); -it("Should remove values from the cache", () => { - const cache = createCache(); +it('Should remove values from the cache', () => { + const cache = createCache(); - cache.add("123", "Matt"); - cache.remove("123"); + cache.add('123', 'Matt'); + cache.remove('123'); - expect(cache.cache["123"]).toEqual(undefined); + expect(cache.cache['123']).toEqual(undefined); }); From 2ea20d5f1e5bcaef24a128575c1cff0140451179 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 11:57:45 +0100 Subject: [PATCH 13/19] :sparkles: complete 12-typeof-narrowing problem --- src/12-typeof-narrowing.problem.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/12-typeof-narrowing.problem.ts b/src/12-typeof-narrowing.problem.ts index 7811011a..faccc65a 100644 --- a/src/12-typeof-narrowing.problem.ts +++ b/src/12-typeof-narrowing.problem.ts @@ -1,11 +1,17 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; -const coerceAmount = (amount: number | { amount: number }) => {}; +const coerceAmount = (amount: number | { amount: number }) => { + if (typeof amount === 'object') { + return amount.amount; + } -it("Should return the amount when passed an object", () => { - expect(coerceAmount({ amount: 20 })).toEqual(20); + return amount; +}; + +it('Should return the amount when passed an object', () => { + expect(coerceAmount({ amount: 20 })).toEqual(20); }); -it("Should return the amount when passed a number", () => { - expect(coerceAmount(20)).toEqual(20); +it('Should return the amount when passed a number', () => { + expect(coerceAmount(20)).toEqual(20); }); From e6ea6d8117cfaaca9f8ed170c29f2a0095ca5c3a Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 12:05:26 +0100 Subject: [PATCH 14/19] :sparkles: complete 13-catch-blocks problem --- src/13-catch-blocks.problem.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/13-catch-blocks.problem.ts b/src/13-catch-blocks.problem.ts index 246d1295..e833388d 100644 --- a/src/13-catch-blocks.problem.ts +++ b/src/13-catch-blocks.problem.ts @@ -1,15 +1,17 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; -const tryCatchDemo = (state: "fail" | "succeed") => { - try { - if (state === "fail") { - throw new Error("Failure!"); +const tryCatchDemo = (state: 'fail' | 'succeed') => { + try { + if (state === 'fail') { + throw new Error('Failure!'); + } + } catch (e) { + if (e instanceof Error) { + return e.message; + } } - } catch (e) { - return e.message; - } }; -it("Should return the message when it fails", () => { - expect(tryCatchDemo("fail")).toEqual("Failure!"); +it('Should return the message when it fails', () => { + expect(tryCatchDemo('fail')).toEqual('Failure!'); }); From abd012a27d2ba29f0d32771cfd34722449730aec Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 12:12:33 +0100 Subject: [PATCH 15/19] :sparkles: complete 14-extends problem --- src/14-extends.problem.ts | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/14-extends.problem.ts b/src/14-extends.problem.ts index e4689052..0a7e1975 100644 --- a/src/14-extends.problem.ts +++ b/src/14-extends.problem.ts @@ -1,30 +1,29 @@ -import { Equal, Expect } from "./helpers/type-utils"; +import { Equal, Expect } from './helpers/type-utils'; /** * Here, the id property is shared between all three * interfaces. Can you find a way to refactor this to * make it more DRY? */ - -interface User { - id: string; - firstName: string; - lastName: string; +interface Base { + id: string; +} +interface User extends Base { + firstName: string; + lastName: string; } -interface Post { - id: string; - title: string; - body: string; +interface Post extends Base { + title: string; + body: string; } -interface Comment { - id: string; - comment: string; +interface Comment extends Base { + comment: string; } type tests = [ - Expect>, - Expect>, - Expect>, + Expect>, + Expect>, + Expect> ]; From 23e1dd4c48eb60e662db09e2c46d75cf0a42255b Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 12:17:20 +0100 Subject: [PATCH 16/19] :sparkles: complete 15-intersection problem --- src/15-intersection.problem.ts | 38 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/15-intersection.problem.ts b/src/15-intersection.problem.ts index a6e5bc7d..5ea86601 100644 --- a/src/15-intersection.problem.ts +++ b/src/15-intersection.problem.ts @@ -1,32 +1,32 @@ interface User { - id: string; - firstName: string; - lastName: string; + id: string; + firstName: string; + lastName: string; } interface Post { - id: string; - title: string; - body: string; + id: string; + title: string; + body: string; } /** * How do we type this return statement so it's both * User AND { posts: Post[] } */ -export const getDefaultUserAndPosts = (): unknown => { - return { - id: "1", - firstName: "Matt", - lastName: "Pocock", - posts: [ - { - id: "1", - title: "How I eat so much cheese", - body: "It's pretty edam difficult", - }, - ], - }; +export const getDefaultUserAndPosts = (): User & { posts: Post[] } => { + return { + id: '1', + firstName: 'Uchenna', + lastName: 'Egbo', + posts: [ + { + id: '1', + title: 'How I learnt Typescript', + body: "It's pretty damn easy", + }, + ], + }; }; const userAndPosts = getDefaultUserAndPosts(); From 946f9417b1763377d2376d64e7e28cc513efc76f Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 12:39:44 +0100 Subject: [PATCH 17/19] :sparkles: complete 16-omit-and-pick problem --- src/16-omit-and-pick.problem.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/16-omit-and-pick.problem.ts b/src/16-omit-and-pick.problem.ts index 8d75c8b9..f1a44750 100644 --- a/src/16-omit-and-pick.problem.ts +++ b/src/16-omit-and-pick.problem.ts @@ -1,9 +1,9 @@ -import { Equal, Expect } from "./helpers/type-utils"; +import { Equal, Expect } from './helpers/type-utils'; interface User { - id: string; - firstName: string; - lastName: string; + id: string; + firstName: string; + lastName: string; } /** @@ -11,6 +11,6 @@ interface User { * firstName and lastName properties of User? */ -type MyType = unknown; +type MyType = Omit; type tests = [Expect>]; From bc1db97cf2f5399d06848fe18b86810d44f2021a Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 16:20:21 +0100 Subject: [PATCH 18/19] :sparkles: complete 17-function-type problem --- src/17-function-types.problem.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/17-function-types.problem.ts b/src/17-function-types.problem.ts index a228d9e7..b9146012 100644 --- a/src/17-function-types.problem.ts +++ b/src/17-function-types.problem.ts @@ -1,20 +1,17 @@ -import { Equal, Expect } from "./helpers/type-utils"; +import { Equal, Expect } from './helpers/type-utils'; -/** - * How do we type onFocusChange? - */ -const addListener = (onFocusChange: unknown) => { - window.addEventListener("focus", () => { - onFocusChange(true); - }); +const addListener = (onFocusChange: (isFocused: boolean) => void) => { + window.addEventListener('focus', () => { + onFocusChange(true); + }); - window.addEventListener("blur", () => { - onFocusChange(false); - }); + window.addEventListener('blur', () => { + onFocusChange(false); + }); }; addListener((isFocused) => { - console.log({ isFocused }); + console.log({ isFocused }); - type tests = [Expect>]; + type tests = [Expect>]; }); From 07aa000175a096c00cbda718d67bade4b08537b0 Mon Sep 17 00:00:00 2001 From: yuhcee Date: Sat, 5 Oct 2024 16:43:32 +0100 Subject: [PATCH 19/19] :sparkles: complete 18-function-type-with-promises problem --- ...18-function-types-with-promises.problem.ts | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/18-function-types-with-promises.problem.ts b/src/18-function-types-with-promises.problem.ts index 086ad34f..7cad41eb 100644 --- a/src/18-function-types-with-promises.problem.ts +++ b/src/18-function-types-with-promises.problem.ts @@ -1,35 +1,32 @@ -import { expect, it } from "vitest"; +import { expect, it } from 'vitest'; interface User { - id: string; - firstName: string; - lastName: string; + id: string; + firstName: string; + lastName: string; } -const createThenGetUser = async ( - createUser: unknown, - getUser: unknown, -): Promise => { - const userId: string = await createUser(); +const createThenGetUser = async (createUser: () => Promise, getUser: (id: string) => Promise): Promise => { + const userId: string = await createUser(); - const user = await getUser(userId); + const user = await getUser(userId); - return user; + return user; }; -it("Should create the user, then get them", async () => { - const user = await createThenGetUser( - async () => "123", - async (id) => ({ - id, - firstName: "Matt", - lastName: "Pocock", - }), - ); +it('Should create the user, then get them', async () => { + const user = await createThenGetUser( + async () => '123', + async (id) => ({ + id, + firstName: 'Matt', + lastName: 'Pocock', + }) + ); - expect(user).toEqual({ - id: "123", - firstName: "Matt", - lastName: "Pocock", - }); + expect(user).toEqual({ + id: '123', + firstName: 'Matt', + lastName: 'Pocock', + }); });