Exercises answers - Problems
Chapter 3: TypeScript for real-world applications
Chapters - Table of contents
1import { compareDesc } from 'date-fns'
2
3////////////////////////////////
4// 1. Find the issue in the code below
5
6interface ConcatValues {
7  (a: string, b: string): string
8  (a: string, b: number): string
9  (a: number, b: number): string
10  (a: number, b: string): string
11}
12
13const concatValues: ConcatValues = (a: number | string, b: number | string) =>
14  [a, b].join('')
15
16// SOLUTION:
17//  (no need for overloads here)
18//
19// const concatValues = (a: number | string, b: number | string) => [a, b].join('')
20
21////////////////////////////////
22// 2. Find the issue in the code below
23
24interface RefObject {
25  readonly current: any
26}
27interface MutableRefObject {
28  current: any
29}
30
31function useRef(initialValue: any): RefObject
32function useRef(): MutableRefObject
33function useRef(ref: any): RefObject {
34  return { current: null }
35}
36
37// SOLUTION:
38//  (useRef() `ref` argument should be optional)
39//
40// function useRef(ref?: any): RefObject {
41//   return { current: null }
42// }
43
44////////////////////////////////
45// 3. Find the issue in the function declaration below
46
47function customDateSort(
48  items: { createdAt: Date; updatedAt?: Date }[]
49): { createdAt: Date; updatedAt?: Date }[] {
50  return items.sort((a, b) =>
51    compareDesc(a.updatedAt || a.createdAt, b.updatedAt || b.createdAt)
52  )
53}
54
55customDateSort([{ createdAt: new Date(), title: 'Blog post 1' }])
56
57// SOLUTION:
58//  (we need a generic with constraint for more flexibility)
59//
60// function customDateSort<T extends { createdAt: Date; updatedAt?: Date }>(
61//   items: T[]
62// ): T[] {
63//   return items.sort((a, b) =>
64//     compareDesc(a.updatedAt || a.createdAt, b.updatedAt || b.createdAt)
65//   )
66// }
67
68////////////////////////////////
69// 5. Find the issue in the code below
70
71type CustomPick<T, K> = {
72  [k in K]: T[k]
73}
74
75interface User {
76  id: string
77  firstName: string
78  lastName: string
79  address: string
80  zipcode: string
81}
82
83type UserAdress = CustomPick<User, 'address' | 'zipcode'>
84// `UserAdress` is
85// {
86//   address: string
87//   zipcode: string
88// }
89
90// SOLUTION:
91//  (we should add a constraint to `K`)
92// type CustomPick<T, K extends keyof T> = {
93//   [k in K]: T[k]
94// }
95
96////////////////////////////////
97// 6. Find the issue in the code below
98
99type CustomPartial<T> = {
100  [k in T]?: T[k]
101}
102
103interface User {
104  id: string
105  firstName: string
106  lastName: string
107  address: string
108  zipcode: string
109}
110
111type UserAdress = CustomPartial<User>
112// `UserAdress` is
113// {
114//  id?: string
115//  firstName?: string
116//  lastName?: string
117//  address?: string
118//  zipcode?: string
119// }
120
121// SOLUTION:
122//  k should be from `keyof T`
123//
124// type CustomPartial<T> = {
125//   [k in keyof T]?: T[k]
126// }
127
We use cookies to collect statistics through Google Analytics.
Do not track
 
Allow cookies