forked from Make-School-Labs/few-2.1-typescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample-14.ts
30 lines (25 loc) · 945 Bytes
/
example-14.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 🧠 These functions use callbacks as parameters.
// Your job is to add proper type annotations for the callback functions.
// 📚 Reference: https://www.typescriptlang.org/docs/handbook/2/functions.html#function-types
// ✅ Task 1: Add a type for a callback that takes a string
function callYouLater(callback, time: number): void {
setTimeout(() => {
callback('What it be like?');
}, time);
}
// ✅ Task 2: Add a type for a callback that takes an object with `success` and `probability`
function callMeMaybe(callback, probability: number): void {
setTimeout(() => {
if (Math.random() * 100 < probability) {
return callback({ success: true, probability });
}
callback({ success: false, probability });
}, 1000);
}
// 💡 Optional Challenge:
// - Create a type alias for the callback used in callMeMaybe
// - Write a test function that uses one or both of these!
export {
callYouLater,
callMeMaybe
}