TypeScript Quiz for JavaScript Devs
by Kwiz
Qs
15
Difficulty
Junior
Plays
0
Record
n/a
Description

This TypeScript quiz is for JavaScript developers dipping their toes into the language and trying to understand the basics of the static typechecker. The questions in this quiz are based on the (must-read) TypeScript Handbook, in particular the TypeScript for JS Programmers section: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html

Question Preview
What annotation do we place after the variable declaration in the following code in order to add explicit types via the User interface?
interface User {
  name: string;
  id: number;
}

const user/* ? */ = {
  name: "Kwiz",
  id: 1
}
Question Preview
What will TypeScript do based on the following code?
interface User {
  name: string;
  id: number;
}

const user: User = {
  username: "Kwiz",
  id: 1
}
Question Preview
TypeScript supports classes and object-oriented programming
Question Preview
How would we correctly use an interface declaration when instantiating the following class UserAccount?
interface User {
  name: string;
  id: number;
}

class UserAccount {
  name: string;
  id: number;

  constructor(name: string, id: number) {
    this.name = name;
    this.id = id;
  }
}
Question Preview
We can user TypeScript interfaces to annotate parameters and return values to functions
function getAdminUser(): User {
  //...
}

function deleteUser(user: User) {
  // ...
}
Question Preview
Select the two popular ways to create complex types in TypeScript by combining simple ones
Question Preview
How would we declare a boolean type MyBool with a union?
Question Preview
Select all of the valid values for the following WindowStates union type
type WindowStates = "open" | "closed" | "minimized";
Question Preview
The following function getLength takes one argument that can be one of two possible types. Select the types that this function can be passed.
function getLength(obj: string | string[]) {
  return obj.length;
}
Question Preview
An array with ________ can describe the _________ that the array contains
Question Preview
What is the syntax of an array with generics that can only contain numbers?
Question Preview
An array without generics could contain __________
Question Preview
Select the appropriate description of the following code
type ObjectWithNameArray = Array<{ name: string }>;
Question Preview
One of TypeScript’s core principles is that type checking focuses on the shape that values have. This is sometimes called... (Two correct answers)
Question Preview
The goal of TypeScript is to be a ______________ for JavaScript programs