Skip to content

T010 Working with types and variables

In this assignment, you’ll get hands-on experience with TypeScript, a statically typed superset of JavaScript. You’ll explore basic variable declarations, type annotations, and operations.

Setup

  1. Create a new TypeScript file

Number Operations

  1. Declare a variable named number of type number and assign it the value 10.
  2. Increment the number variable using both ++ and +=.
  3. Add 5 to the number variable.
  4. Subtract 2 from the number variable.
  5. Multiply the number variable by 3.
  6. Divide the number variable by 2.
  7. Print number using console.log.

String Operations

  1. Declare a variable named text of type string and assign it the value “test”.
  2. Print text using console.log.
  3. Declare another variable named text2 of type string and assign it the value of text in uppercase.
  4. Print text2 using console.log.
  5. Declare a variable called count of type number and assign it the value 10.
  6. Using a template string, create a variable template of type string that reads “There are a total of 10 items”.
  7. Print template with console.log.

Boolean Variable

  1. Declare a Boolean variable named isTrue of type boolean and set its value to true.
  2. Print isTrue using console.log.

Date Variable

  1. Declare a variable named currentDate of type Date and set its value to the current date and time.
  2. Print currentDate using console.log.

Challenge: Type Assertions

  1. Declare a variable named unknownType of type any and assign it the value “12345”.
  2. Convert unknownType to a number using type assertion.
  3. Print the converted value using console.log.

Challenge: Array Operations

  1. Declare an array named colors of type string[] and assign it the values “red”, “green”, and “blue”.
  2. Add “yellow” to the end of the colors array.
  3. Remove the first element from the colors array.
  4. Print the length of the colors array using console.log.

Challenge: Enum Usage

  1. Declare an enum named Days with values “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, and “Sunday”.
  2. Declare a variable named today of type Days and assign it the value Days.Wednesday.
  3. Print the value of today using console.log.

Challenge: Tuple Operations

  1. Declare a tuple named student of type [string, number] and assign it the values “John Doe” and 25.
  2. Print the student’s name and age using console.log.

Challenge: Object Structure with Interface

  • Declare an interface named Book with the following properties:

    • title: string
    • author: string
    • publishedYear: number
    • isBestSeller: boolean
  • Create an object named myFavoriteBook that adheres to the Book interface with the following details:

    • title: “The Great Adventure”
    • author: “Jane Smith”
    • publishedYear: 2015
    • isBestSeller: true
  • Print the title and author of myFavoriteBook using console.log.

Once you’ve completed the assignment, you can compile the TypeScript file using the TypeScript compiler (tsc) to generate the corresponding JavaScript file. This will give you a chance to see how TypeScript transpiles to JavaScript.

Solution
let number: number = 10;
number++;           // Increment by 1
number += 1;        // Increment by 1
number += 5;        // Add 5
number -= 2;        // Subtract 2
number *= 3;        // Multiply by 3
number /= 2;        // Divide by 2
console.log(number);

let text: string = "test";
console.log(text);

let text2: string = text.toUpperCase();
console.log(text2);

let count: number = 10;
let template: string = `There are a total of ${count} items`;
console.log(template);

let isTrue: boolean = true;
console.log(isTrue);

let currentDate: Date = new Date();
console.log(currentDate);

let unknownType: any = "12345";
let convertedNumber: number = <number><any>unknownType; // Type assertion
console.log(convertedNumber);

let colors: string[] = ["red", "green", "blue"];
colors.push("yellow");
colors.shift();
console.log(colors.length); // Expected output: 3

enum Days {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

let today: Days = Days.Wednesday;
console.log(today); // Expected output: 2 (since enums are zero-based by default)

let student: [string, number] = ["John Doe", 25];
console.log(`Name: ${student[0]}, Age: ${student[1]}`); // Expected output: Name: John Doe, Age: 25

// Declaring the Book interface
interface Book {
    title: string;
    author: string;
    publishedYear: number;
    isBestSeller: boolean;
}

// Creating an object that adheres to the Book interface
let myFavoriteBook: Book = {
    title: "The Great Adventure",
    author: "Jane Smith",
    publishedYear: 2015,
    isBestSeller: true
};

// Printing the title and author of the book
console.log(`Title: ${myFavoriteBook.title}, Author: ${myFavoriteBook.author}`); // Expected output: Title: The Great Adventure, Author: Jane Smith