-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
41 lines (31 loc) · 1.07 KB
/
index.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
31
32
33
34
35
36
37
38
39
40
41
// This is a tiny app that is going to make an external
// API request and print out some JSON to console
// ///////////////////////////////////////
import axios from 'axios';
const url = 'https://jsonplaceholder.typicode.com/todos/1';
// Intefaces help define structure of objects in TypeScript
interface Todo {
id: number;
title: string;
completed: boolean;
}
axios.get(url).then((response) => {
// const todo = response.data;
// Type script way of doing
const todo = response.data as Todo;
// This 3 lines print out undefined only after execution if we use plain JS. But with TS these line shout at you saying something is wrong as we are using interfaces.
// const ID = todo.ID;
// const title = todo.Title;
// const finished = todo.finished;
const id = todo.id;
const title = todo.title;
const completed = todo.completed;
logTodo(id, title, completed);
});
const logTodo = (id: number, title: string, completed: boolean) => {
console.log(`
The Todo with ID : ${id}
Has a title of: ${title}
Is it finished? ${completed}
`);
};