Skip to content

Commit

Permalink
2023-08-16T14:03:22.536Z
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Aug 16, 2023
1 parent fb92816 commit a868d1e
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/020-objects/065-common-keys-of-unions-of-objects.problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ type Organisation = {
imageId: string;
};

type Product = {
id: string;
name: string;
price: number;
imageId: string;
};

const getAvatarImage = (entity: unknown) => {
{
// Should not be able to access properties that are
Expand All @@ -24,6 +31,9 @@ const getAvatarImage = (entity: unknown) => {

// @ts-expect-error
entity.address;

// @ts-expect-error
entity.price;
}

return {
Expand Down Expand Up @@ -59,3 +69,17 @@ it("Should work for an organisation", () => {
alt: "Total TypeScript Avatar",
});
});

it("Should work for a product", () => {
const result = getAvatarImage({
id: "1",
name: "TypeScript Mug",
price: 10,
imageId: "abc123",
});

expect(result).toEqual({
url: "https://via.placeholder.com/abc123",
alt: "TypeScript Mug Avatar",
});
});
26 changes: 25 additions & 1 deletion src/020-objects/065-common-keys-of-unions-of-objects.solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ type Organisation = {
imageId: string;
};

const getAvatarImage = (entity: User | Organisation) => {
type Product = {
id: string;
name: string;
price: number;
imageId: string;
};

const getAvatarImage = (entity: User | Organisation | Product) => {
{
// Should not be able to access properties that are
// not common to both types
Expand All @@ -24,6 +31,9 @@ const getAvatarImage = (entity: User | Organisation) => {

// @ts-expect-error
entity.address;

// @ts-expect-error
entity.price;
}

return {
Expand Down Expand Up @@ -59,3 +69,17 @@ it("Should work for an organisation", () => {
alt: "Total TypeScript Avatar",
});
});

it("Should work for a product", () => {
const result = getAvatarImage({
id: "1",
name: "TypeScript Mug",
price: 10,
imageId: "abc123",
});

expect(result).toEqual({
url: "https://via.placeholder.com/abc123",
alt: "TypeScript Mug Avatar",
});
});

This file was deleted.

46 changes: 46 additions & 0 deletions src/020-objects/068-omit-cant-distribute.explainer.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
type User = {
id: string;
name: string;
age: number;
imageId: string;
};

type Organisation = {
id: string;
name: string;
address: string;
imageId: string;
};

type Product = {
id: string;
name: string;
price: number;
imageId: string;
};

type Entity = User | Organisation | Product;

type EntityWithoutId = Omit<Entity, "id">;
// ^?

type test = Expect<
Equal<
EntityWithoutId,
| {
name: string;
age: number;
imageId: string;
}
| {
name: string;
address: string;
imageId: string;
}
| {
name: string;
price: number;
imageId: string;
}
>
>;
50 changes: 50 additions & 0 deletions src/020-objects/068-omit-cant-distribute.explainer.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
type User = {
id: string;
name: string;
age: number;
imageId: string;
};

type Organisation = {
id: string;
name: string;
address: string;
imageId: string;
};

type Product = {
id: string;
name: string;
price: number;
imageId: string;
};

type Entity = User | Organisation | Product;

type DistributiveOmit<T, K extends PropertyKey> = T extends any
? Omit<T, K>
: never;

type EntityWithoutId = DistributiveOmit<Entity, "id">;
// ^?

type test = Expect<
Equal<
EntityWithoutId,
| {
name: string;
age: number;
imageId: string;
}
| {
name: string;
address: string;
imageId: string;
}
| {
name: string;
price: number;
imageId: string;
}
>
>;
29 changes: 29 additions & 0 deletions src/020-objects/069-omit-or-pick-more-than-one-thing.problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interface Product {
id: number;
name: string;
price: number;
description: string;
}

const addProduct = (productInfo: unknown) => {
// Do something with the productInfo
};

addProduct({
name: "Book",
description: "A book about Dragons",
});

addProduct({
// @ts-expect-error
id: 1,
name: "Book",
description: "A book about Dragons",
});

addProduct({
// @ts-expect-error
price: 10,
name: "Book",
description: "A book about Dragons",
});
29 changes: 29 additions & 0 deletions src/020-objects/069-omit-or-pick-more-than-one-thing.solution.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interface Product {
id: number;
name: string;
price: number;
description: string;
}

const addProduct = (productInfo: Omit<Product, "id" | "price">) => {
// Do something with the productInfo
};

addProduct({
name: "Book",
description: "A book about Dragons",
});

addProduct({
// @ts-expect-error
id: 1,
name: "Book",
description: "A book about Dragons",
});

addProduct({
// @ts-expect-error
price: 10,
name: "Book",
description: "A book about Dragons",
});
29 changes: 29 additions & 0 deletions src/020-objects/069-omit-or-pick-more-than-one-thing.solution.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interface Product {
id: number;
name: string;
price: number;
description: string;
}

const addProduct = (productInfo: Pick<Product, "name" | "description">) => {
// Do something with the productInfo
};

addProduct({
name: "Book",
description: "A book about Dragons",
});

addProduct({
// @ts-expect-error
id: 1,
name: "Book",
description: "A book about Dragons",
});

addProduct({
// @ts-expect-error
price: 10,
name: "Book",
description: "A book about Dragons",
});

0 comments on commit a868d1e

Please sign in to comment.