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
- Create a new TypeScript file
Number Operations
- Declare a variable named
numberof typenumberand assign it the value 10. - Increment the
numbervariable using both++and+=. - Add 5 to the
numbervariable. - Subtract 2 from the
numbervariable. - Multiply the
numbervariable by 3. - Divide the
numbervariable by 2. - Print
numberusingconsole.log.
String Operations
- Declare a variable named
textof typestringand assign it the value “test”. - Print
textusingconsole.log. - Declare another variable named
text2of typestringand assign it the value oftextin uppercase. - Print
text2usingconsole.log. - Declare a variable called
countof typenumberand assign it the value 10. - Using a template string, create a variable
templateof typestringthat reads “There are a total of 10 items”. - Print
templatewithconsole.log.
Boolean Variable
- Declare a Boolean variable named
isTrueof typebooleanand set its value to true. - Print
isTrueusingconsole.log.
Date Variable
- Declare a variable named
currentDateof typeDateand set its value to the current date and time. - Print
currentDateusingconsole.log.
Challenge: Type Assertions
- Declare a variable named
unknownTypeof typeanyand assign it the value “12345”. - Convert
unknownTypeto a number using type assertion. - Print the converted value using
console.log.
Challenge: Array Operations
- Declare an array named
colorsof typestring[]and assign it the values “red”, “green”, and “blue”. - Add “yellow” to the end of the
colorsarray. - Remove the first element from the
colorsarray. - Print the length of the
colorsarray usingconsole.log.
Challenge: Enum Usage
- Declare an enum named
Dayswith values “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, and “Sunday”. - Declare a variable named
todayof typeDaysand assign it the valueDays.Wednesday. - Print the value of
todayusingconsole.log.
Challenge: Tuple Operations
- Declare a tuple named
studentof type[string, number]and assign it the values “John Doe” and 25. - Print the student’s name and age using
console.log.
Challenge: Object Structure with Interface
-
Declare an interface named
Bookwith the following properties:title: stringauthor: stringpublishedYear: numberisBestSeller: boolean
-
Create an object named
myFavoriteBookthat adheres to theBookinterface with the following details:title: “The Great Adventure”author: “Jane Smith”publishedYear: 2015isBestSeller: true
-
Print the title and author of
myFavoriteBookusingconsole.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