Skip to content
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

feat: add additional solutions to 06 exercise #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
89 changes: 89 additions & 0 deletions src/01-generics-intro/06-generic-mapper.solution.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { expect, it } from "vitest";
import { Equal, Expect } from "../helpers/type-utils";

interface User {
firstName: string;
lastName: string;
}

export const concatenateFirstNameAndLastName = <T extends User>(user: T) => {
return {
...user,
fullName: `${user.firstName} ${user.lastName}`,
};
};

it("Should add fullName to an object which only contains firstName and lastName", () => {
const users = [
{
firstName: "Matt",
lastName: "Pocock",
},
];

const newUsers = users.map(concatenateFirstNameAndLastName);

expect(newUsers).toEqual([
{
firstName: "Matt",
lastName: "Pocock",
fullName: "Matt Pocock",
},
]);

type tests = [
Expect<
Equal<
typeof newUsers,
Array<{ firstName: string; lastName: string } & { fullName: string }>
>
>
];
});

it("Should retain other properties passed in", () => {
const users = [
{
id: 1,
firstName: "Matt",
lastName: "Pocock",
},
];

const newUsers = users.map(concatenateFirstNameAndLastName);

expect(newUsers).toEqual([
{
id: 1,
firstName: "Matt",
lastName: "Pocock",
fullName: "Matt Pocock",
},
]);

type tests = [
Expect<
Equal<
typeof newUsers,
Array<
{ id: number; firstName: string; lastName: string } & {
fullName: string;
}
>
>
>
];
});

it("Should fail when the object passed in does not contain firstName", () => {
const users = [
{
firstName: "Matt",
},
];

const newUsers = users.map(
// @ts-expect-error
concatenateFirstNameAndLastName
);
});
94 changes: 94 additions & 0 deletions src/01-generics-intro/06-generic-mapper.solution.3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { expect, it } from "vitest";
import { Equal, Expect } from "../helpers/type-utils";

interface FirstName {
firstName: string;
}

interface LastName {
lastName: string;
}

export const concatenateFirstNameAndLastName = <T extends FirstName & LastName>(
user: T
) => {
return {
...user,
fullName: `${user.firstName} ${user.lastName}`,
};
};

it("Should add fullName to an object which only contains firstName and lastName", () => {
const users = [
{
firstName: "Matt",
lastName: "Pocock",
},
];

const newUsers = users.map(concatenateFirstNameAndLastName);

expect(newUsers).toEqual([
{
firstName: "Matt",
lastName: "Pocock",
fullName: "Matt Pocock",
},
]);

type tests = [
Expect<
Equal<
typeof newUsers,
Array<{ firstName: string; lastName: string } & { fullName: string }>
>
>
];
});

it("Should retain other properties passed in", () => {
const users = [
{
id: 1,
firstName: "Matt",
lastName: "Pocock",
},
];

const newUsers = users.map(concatenateFirstNameAndLastName);

expect(newUsers).toEqual([
{
id: 1,
firstName: "Matt",
lastName: "Pocock",
fullName: "Matt Pocock",
},
]);

type tests = [
Expect<
Equal<
typeof newUsers,
Array<
{ id: number; firstName: string; lastName: string } & {
fullName: string;
}
>
>
>
];
});

it("Should fail when the object passed in does not contain firstName", () => {
const users = [
{
firstName: "Matt",
},
];

const newUsers = users.map(
// @ts-expect-error
concatenateFirstNameAndLastName
);
});