diff --git a/src/01-generics-intro/06-generic-mapper.solution.ts b/src/01-generics-intro/06-generic-mapper.solution.1.ts similarity index 100% rename from src/01-generics-intro/06-generic-mapper.solution.ts rename to src/01-generics-intro/06-generic-mapper.solution.1.ts diff --git a/src/01-generics-intro/06-generic-mapper.solution.2.ts b/src/01-generics-intro/06-generic-mapper.solution.2.ts new file mode 100644 index 0000000..ebacd12 --- /dev/null +++ b/src/01-generics-intro/06-generic-mapper.solution.2.ts @@ -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 = (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 + ); +}); diff --git a/src/01-generics-intro/06-generic-mapper.solution.3.ts b/src/01-generics-intro/06-generic-mapper.solution.3.ts new file mode 100644 index 0000000..7455b96 --- /dev/null +++ b/src/01-generics-intro/06-generic-mapper.solution.3.ts @@ -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 = ( + 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 + ); +});