Skip to content

Commit 49c9ec8

Browse files
committed
Added 14-16
1 parent 0e61654 commit 49c9ec8

11 files changed

+176
-8
lines changed

notes/FUTURE.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
## Things to add
22

3-
- any vs unknown (use casting an error in a catch block as an example)
4-
- intersection types
53
- classes
6-
- interface extends
74
- Omit, Partial, Pick, Required, Record (object-based utility types)
85
- ReadonlyArray
9-
- typeof narrowing
10-
- instanceof narrowing
11-
- limits of typeof narrowing (unknown and object)
126

137
## Things NOT to add
148

159
These things are out-of-scope, or will be included in other modules
1610

11+
- limits of typeof narrowing (unknown and object)
1712
- Type predicates
1813
- Using external libraries and @types
1914
- discriminated unions

src/10-set.problem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ it("Should give a type error when you try to pass a non-string", () => {
1919
it("Should be typed as an array of strings", () => {
2020
const guitaristsAsArray = Array.from(guitarists);
2121

22-
type cases = [Expect<Equal<typeof guitaristsAsArray, string[]>>];
22+
type tests = [Expect<Equal<typeof guitaristsAsArray, string[]>>];
2323
});

src/10-set.solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ it("Should give a type error when you try to pass a non-string", () => {
1919
it("Should be typed as an array of strings", () => {
2020
const guitaristsAsArray = Array.from(guitarists);
2121

22-
type cases = [Expect<Equal<typeof guitaristsAsArray, string[]>>];
22+
type tests = [Expect<Equal<typeof guitaristsAsArray, string[]>>];
2323
});

src/14-extends.problem.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Equal, Expect } from "./helpers/type-utils";
2+
3+
/**
4+
* Here, the id property is shared between all three
5+
* interfaces. Can you find a way to refactor this to
6+
* make it more DRY?
7+
*/
8+
9+
interface User {
10+
id: string;
11+
firstName: string;
12+
lastName: string;
13+
}
14+
15+
interface Post {
16+
id: string;
17+
title: string;
18+
body: string;
19+
}
20+
21+
interface Comment {
22+
id: string;
23+
comment: string;
24+
}
25+
26+
type tests = [
27+
Expect<Equal<User, { id: string; firstName: string; lastName: string }>>,
28+
Expect<Equal<Post, { id: string; title: string; body: string }>>,
29+
Expect<Equal<Comment, { id: string; comment: string }>>,
30+
];

src/14-extends.solution.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Equal, Expect } from "./helpers/type-utils";
2+
3+
/**
4+
* Here, the id property is shared between all three
5+
* interfaces. Can you find a way to refactor this to
6+
* make it more DRY?
7+
*/
8+
9+
interface Base {
10+
id: string;
11+
}
12+
13+
interface User extends Base {
14+
firstName: string;
15+
lastName: string;
16+
}
17+
18+
interface Post extends Base {
19+
title: string;
20+
body: string;
21+
}
22+
23+
interface Comment extends Base {
24+
comment: string;
25+
}
26+
27+
type tests = [
28+
Expect<Equal<User, { id: string; firstName: string; lastName: string }>>,
29+
Expect<Equal<Post, { id: string; title: string; body: string }>>,
30+
Expect<Equal<Comment, { id: string; comment: string }>>,
31+
];

src/15-intersection.problem.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
interface User {
2+
id: string;
3+
firstName: string;
4+
lastName: string;
5+
}
6+
7+
interface Post {
8+
id: string;
9+
title: string;
10+
body: string;
11+
}
12+
13+
/**
14+
* How do we type this return statement so it's both
15+
* User AND { posts: Post[] }
16+
*/
17+
export const getDefaultUserAndPosts = (): unknown => {
18+
return {
19+
id: "1",
20+
firstName: "Matt",
21+
lastName: "Pocock",
22+
posts: [
23+
{
24+
id: "1",
25+
title: "How I eat so much cheese",
26+
body: "It's pretty edam difficult",
27+
},
28+
],
29+
};
30+
};
31+
32+
const userAndPosts = getDefaultUserAndPosts();
33+
34+
console.log(userAndPosts.posts[0]);

src/15-intersection.solution.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
interface User {
2+
id: string;
3+
firstName: string;
4+
lastName: string;
5+
}
6+
7+
interface Post {
8+
id: string;
9+
title: string;
10+
body: string;
11+
}
12+
13+
export const getDefaultUserAndPosts = (): User & { posts: Post[] } => {
14+
return {
15+
id: "1",
16+
firstName: "Matt",
17+
lastName: "Pocock",
18+
posts: [
19+
{
20+
id: "1",
21+
title: "How I eat so much cheese",
22+
body: "It's pretty edam difficult",
23+
},
24+
],
25+
};
26+
};
27+
28+
const userAndPosts = getDefaultUserAndPosts();
29+
30+
console.log(userAndPosts.posts[0]);

src/16-omit-and-pick.problem.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Equal, Expect } from "./helpers/type-utils";
2+
3+
interface User {
4+
id: string;
5+
firstName: string;
6+
lastName: string;
7+
}
8+
9+
/**
10+
* How do we create a new object type with _only_ the
11+
* firstName and lastName properties of User?
12+
*/
13+
14+
type MyType = unknown;
15+
16+
type tests = [Expect<Equal<MyType, { firstName: string; lastName: string }>>];

src/16-omit-and-pick.solution.1.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Equal, Expect } from "./helpers/type-utils";
2+
3+
interface User {
4+
id: string;
5+
firstName: string;
6+
lastName: string;
7+
}
8+
9+
/**
10+
* How do we create a new object type with _only_ the
11+
* firstName and lastName properties of User?
12+
*/
13+
14+
type MyType = Omit<User, "id">;
15+
16+
type tests = [Expect<Equal<MyType, { firstName: string; lastName: string }>>];

src/16-omit-and-pick.solution.2.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Equal, Expect } from "./helpers/type-utils";
2+
3+
interface User {
4+
id: string;
5+
firstName: string;
6+
lastName: string;
7+
}
8+
9+
/**
10+
* How do we create a new object type with _only_ the
11+
* firstName and lastName properties of User?
12+
*/
13+
14+
type MyType = Pick<User, "firstName" | "lastName">;
15+
16+
type tests = [Expect<Equal<MyType, { firstName: string; lastName: string }>>];

src/98-extends.problem.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)