Skip to content

Commit

Permalink
2023-08-16T09:00:43.457Z
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Aug 16, 2023
1 parent f40f234 commit 65e21f8
Show file tree
Hide file tree
Showing 67 changed files with 533 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
type User = {
id: string;
createdAt: Date;
name: string;
email: string;
};

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

type tests = [
Expect<
Extends<
User,
{
id: string;
createdAt: Date;
name: string;
email: string;
}
>
>,
Expect<
Extends<
Product,
{
id: string;
createdAt: Date;
name: string;
price: number;
}
>
>,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
type BaseEntity = {
id: string;
createdAt: Date;
};

type User = {
name: string;
email: string;
} & BaseEntity;

type Product = {
name: string;
price: number;
} & BaseEntity;

type tests = [
Expect<
Extends<
User,
{
id: string;
createdAt: Date;
name: string;
email: string;
}
>
>,
Expect<
Extends<
Product,
{
id: string;
createdAt: Date;
name: string;
price: number;
}
>
>,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
type BaseEntity = {
id: string;
createdAt: Date;
};

type User = {
name: string;
email: string;
} & BaseEntity;

type Product = {
name: string;
price: number;
} & BaseEntity;

type tests = [
Expect<
Extends<
User,
{
id: string;
createdAt: Date;
name: string;
email: string;
}
>
>,
Expect<
Extends<
Product,
{
id: string;
createdAt: Date;
name: string;
price: number;
}
>
>,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
interface BaseEntity {
id: string;
createdAt: Date;
}

interface User extends BaseEntity {
name: string;
email: string;
}

interface Product extends BaseEntity {
name: string;
price: number;
}

type tests = [
Expect<
Extends<
User,
{
id: string;
createdAt: Date;
name: string;
email: string;
}
>
>,
Expect<
Extends<
Product,
{
id: string;
createdAt: Date;
name: string;
price: number;
}
>
>,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
interface WithId {
id: string;
}

interface WithCreatedAt {
createdAt: Date;
}

interface User extends WithId, WithCreatedAt {
name: string;
email: string;
}

interface Product extends WithId, WithCreatedAt {
name: string;
price: number;
}

type tests = [
Expect<
Extends<
User,
{
id: string;
createdAt: Date;
name: string;
email: string;
}
>
>,
Expect<
Extends<
Product,
{
id: string;
createdAt: Date;
name: string;
price: number;
}
>
>,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// https://github.com/microsoft/TypeScript/wiki/Performance#preferring-interfaces-over-intersections
5 changes: 5 additions & 0 deletions src/020-sets-of-types/060-index-signatures.problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const scores = {};

scores.math = 95;
scores.english = 90;
scores.science = 85;
9 changes: 9 additions & 0 deletions src/020-sets-of-types/060-index-signatures.solution.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Scores = {
[key: string]: number;
};

const scores: Scores = {};

scores.math = 95;
scores.english = 90;
scores.science = 85;
9 changes: 9 additions & 0 deletions src/020-sets-of-types/060-index-signatures.solution.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Scores {
[key: string]: number;
}

const scores: Scores = {};

scores.math = 95;
scores.english = 90;
scores.science = 85;
7 changes: 7 additions & 0 deletions src/020-sets-of-types/060-index-signatures.solution.3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const scores: {
[key: string]: number;
} = {};

scores.math = 95;
scores.english = 90;
scores.science = 85;
5 changes: 5 additions & 0 deletions src/020-sets-of-types/060-index-signatures.solution.4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const scores: Record<string, number> = {};

scores.math = 95;
scores.english = 90;
scores.science = 85;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface Scores {
[key: string]: number;
math: number;
english: number;
science: number;
}

// @ts-expect-error science is missing!
const scores: Scores = {
math: 95,
english: 90,
};

scores.athletics = 100;
scores.french = 75;
scores.spanish = 70;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface Scores {
[key: string]: number;
math: number;
english: number;
science: number;
}

// @ts-expect-error science is missing!
const scores: Scores = {
math: 95,
english: 90,
};

scores.athletics = 100;
scores.french = 75;
scores.spanish = 70;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface RequiredScores {
math: number;
english: number;
science: number;
}

interface Scores extends RequiredScores {
[key: string]: number;
}

// @ts-expect-error science is missing!
const scores: Scores = {
math: 95,
english: 90,
};

scores.athletics = 100;
scores.french = 75;
scores.spanish = 70;
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface Logger {
log(message: string, level: number): void;
}

interface Logger {
log(message: string): void;
}

const myLogger: Logger = {
log: (message: string) => {
console.log(message);
},
};

myLogger.log(
"My message",
// @ts-expect-error Level is NOT needed
123,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Logger = {
log(message: string): void;
};

const myLogger: Logger = {
log: (message: string) => {
console.log(message);
},
};

myLogger.log(
"My message",
// @ts-expect-error Level is NOT needed
123,
);
18 changes: 18 additions & 0 deletions src/020-sets-of-types/064-pick-type-helper.problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interface User {
id: string;
name: string;
email: string;
role: string;
}

const fetchUser = async () => {
const response = await fetch("/api/user");
const user = await response.json();
return user;
};

const example = async () => {
const user = await fetchUser();

type test = Expect<Equal<typeof user, { name: string; email: string }>>;
};
21 changes: 21 additions & 0 deletions src/020-sets-of-types/064-pick-type-helper.solution.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface NameAndEmail {
name: string;
email: string;
}

interface User extends NameAndEmail {
id: string;
role: string;
}

const fetchUser = async (): Promise<NameAndEmail> => {
const response = await fetch("/api/user");
const user = await response.json();
return user;
};

const example = async () => {
const user = await fetchUser();

type test = Expect<Equal<typeof user, { name: string; email: string }>>;
};
20 changes: 20 additions & 0 deletions src/020-sets-of-types/064-pick-type-helper.solution.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
interface User {
id: string;
name: string;
email: string;
role: string;
}

type OnlyNameAndEmail = Pick<User, "name" | "email">;

const fetchUser = async (): Promise<OnlyNameAndEmail> => {
const response = await fetch("/api/user");
const user = await response.json();
return user;
};

const example = async () => {
const user = await fetchUser();

type test = Expect<Equal<typeof user, { name: string; email: string }>>;
};
Loading

0 comments on commit 65e21f8

Please sign in to comment.