Skip to content

✨ complete 18-function-type-with-promises problem #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"express": "^4.18.1",
"zod": "^3.17.10"
}
}
}
12 changes: 6 additions & 6 deletions src/01-number.problem.ts
Original file line number Diff line number Diff line change
@@ -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);
});
37 changes: 21 additions & 16 deletions src/02-object-param.problem.ts
Original file line number Diff line number Diff line change
@@ -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);
});
34 changes: 17 additions & 17 deletions src/03-optional-properties.problem.ts
Original file line number Diff line number Diff line change
@@ -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');
});
24 changes: 12 additions & 12 deletions src/04-optional-params.problem.ts
Original file line number Diff line number Diff line change
@@ -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');
});
27 changes: 14 additions & 13 deletions src/05-assigning-types-to-variables.problem.ts
Original file line number Diff line number Diff line change
@@ -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);
});
25 changes: 10 additions & 15 deletions src/06-unions.problem.ts
Original file line number Diff line number Diff line change
@@ -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',
};
42 changes: 21 additions & 21 deletions src/07-arrays.problem.ts
Original file line number Diff line number Diff line change
@@ -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",
},
],
};
51 changes: 29 additions & 22 deletions src/08-function-return-type-annotations.problem.ts
Original file line number Diff line number Diff line change
@@ -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<Post>;
id: number;
firstName: string;
lastName: string;
role: 'admin' | 'user' | 'super-admin';
posts: Array<Post>;
}

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');
});
26 changes: 13 additions & 13 deletions src/09-promises.problem.ts
Original file line number Diff line number Diff line change
@@ -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;
};
27 changes: 13 additions & 14 deletions src/10-set.problem.ts
Original file line number Diff line number Diff line change
@@ -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<string>();

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<Equal<typeof guitaristsAsArray, string[]>>];
type tests = [Expect<Equal<typeof guitaristsAsArray, string[]>>];
});
Loading
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