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..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); }); 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); }); 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'); }); 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'); }); diff --git a/src/05-assigning-types-to-variables.problem.ts b/src/05-assigning-types-to-variables.problem.ts index 50de8989..93baca82 100644 --- a/src/05-assigning-types-to-variables.problem.ts +++ b/src/05-assigning-types-to-variables.problem.ts @@ -1,22 +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 = {}; +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', }; 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", + }, + ], }; 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'); }); 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; }; 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>]; }); 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); }); 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); }); 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!'); }); 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> ]; 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(); 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>]; 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>]; }); 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', + }); }); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy