DEV Community

Abhay Nikam
Abhay Nikam

Posted on

Day 1-2 - Started with 100 days of coding challenge to learn TypeScript

I publicly(on twitter) committed to the 100DaysOfCode Challenge yesterday and started with learning TypeScript.

I am completing a TypeScript course on Udemy - Typescript: The Complete Developer's Guide

Day 1: Started learning TypeScript

Completed with the introduction and basic environment setup for TypeScript.

Day 2: Executed the first TypeScript code.

The main goal of TypeScript is to catch the errors in the development phase. Started with a simple example to fetch static JSON data and pretty-print the response data.

Wrote an interface in TypeScript. Interfaces in TypeScript are used to define the structure of the object. Adding interface helped in catching the error in development if wrong JSON key was used.

Here is the first index.ts I wrote:

import axios from "axios";

const url = "https://jsonplaceholder.typicode.com/todos/1";

interface Todo {
  id: number;
  title: string;
  completed: boolean;
}

axios.get(url).then(response => {
  const todo = response.data as Todo;

  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}
  `);
};

Happy coding fellas.

Top comments (0)