Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
simonplend committed Sep 4, 2024
1 parent 205e507 commit 3d41fb8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
86 changes: 86 additions & 0 deletions 01-composing-types-part-1/samples/03-generic-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,89 @@

console.log(spainLanguages.languages.join(", "));
}

// TODO: More examples

{
type MessageOf<MediumType> = MediumType extends { message: unknown }
? MediumType["message"]
: never;

interface Email {
message: string;
}

interface SMS {
message: string;
}

type EmailMessage = MessageOf<Email>;
// ^?

type SMSMessage = MessageOf<SMS>;
// ^?

type NoMessage = MessageOf<number>;
// ^?
}

// ----

{
type MessageOfV2<MediumType> = MediumType extends {
message: infer MessageType;
}
? MessageType
: never;

interface Email {
message: string;
}

interface SMS {
message: string;
}

type EmailMessageV2 = MessageOfV2<Email>;
// ^?

type SMSMessageV2 = MessageOfV2<SMS>;
// ^?

type NoMessageV2 = MessageOfV2<number>;
// ^?
}

// ----

{
type Photo = {
format: "jpeg" | "png";
quality: "low" | "high";
};

type EditOptions = {
format: "jpeg" | "png";
quality: "low" | "high";
grayscale: boolean;
blur: boolean;
};

type EditPhoto<PhotoType, EditOptionsType> = PhotoType extends {
format: infer Format;
quality: infer Quality;
}
? EditOptionsType extends { format: Format; quality: Quality }
? PhotoType & EditOptionsType
: never
: never;

type EditedPhoto = EditPhoto<Photo, EditOptions>;

const photo: EditedPhoto = {
format: "jpeg",
quality: "high",
grayscale: true,
blur: false,
};
}
3 changes: 3 additions & 0 deletions 01-composing-types-part-2/exercise/start/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@total-typescript/tsconfig/tsc/no-dom/app"
}
6 changes: 4 additions & 2 deletions 02-improving-type-safety/lab/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Lab: Type Safe Creators
# Lab: Type Safe Creators 🎬

> **Section 2: Improving Type Safety**
## Requirements

1. There are eight pesky type errors in the Step 1 section of `start.ts`. Use the `satisfies` operator to help you fix them.
2. In the `for...of` loop in Step 2 section, `Array.isArray` is used to narrow the type for the `creator` variable. Refactor this code to use type predicates to narrow the type instead.
1. Create two type predicate functions: `isDirector()` and `isProducer()`
2. Hint: The return type for these functions should be a type predicate where the type is an intersection type.
2. Hint: The return type for these functions should be a type predicate where the type is an *intersection type*.

0 comments on commit 3d41fb8

Please sign in to comment.