TypeScript Online Test

Welcome to our TypeScript Online Test! This test includes 25 multiple-choice questions that test your knowledge of TypeScript, a special version of JavaScript that helps ensure your code is correct before running it. 

In this test, you'll answer questions about basic and more advanced topics, like how to use types, how to organize your code with classes, and more. Whether you're just starting out or you've been using TypeScript for a while, this test is a great way to see what you know and learn more along the way. Let's get started and see how much you know about TypeScript!

1. What is TypeScript primarily used for?

a) To add static types to JavaScript
b) To execute server-side JavaScript code
c) To add new syntax to JavaScript that browsers can execute
d) To increase the performance of JavaScript in the browser

2. Which TypeScript feature allows you to specify that a parameter or variable could be one of several types?

a) Generics
b) Union types
c) Enums
d) Any type

3. How do you declare a variable whose type is strictly any number or string in TypeScript?

let value: number | string;
a) let value: number | string;
b) let value: any;
c) let value: number && string;
d) let value: mixed;

4. What is the output of the following TypeScript code?

let x: unknown = 'hello';
console.log(typeof x);
a) unknown
b) string
c) error
d) undefined

5. Which of the following TypeScript types is used for a variable that can only have a null or undefined value?

a) void
b) null
c) undefined
d) any

6. What is a TypeScript interface used for?

a) To define a contract for classes to implement
b) To specify that a variable can be any type
c) To encapsulate implementation details of a class
d) To provide optional typing features

7. Which is a valid way to declare a class in TypeScript?

class Person {
    name: string;
    constructor(name: string) {
        this.name = name;
a) class Person { name: string; constructor(name: string) { this.name = name; } }
b) class Person implements name: string { constructor(name) { this.name = name; } }
c) function Person(name: string) { this.name = name; }
d) var Person = { name: string; constructor(name: string) { this.name = name; } }

8. How do you enable strict type-checking in a TypeScript project?

a) Add "strict": true to the tsconfig.json file
b) Run tsc --strict
c) Use the --enable-strict command line flag
d) Write use strict at the top of your TypeScript files

9. Which TypeScript type should be used to indicate that a function never returns?

a) undefined
b) null
c) void
d) never

10. What is the purpose of the as keyword in TypeScript?

a) To perform a runtime check
b) To assert a type
c) To define a variable
d) To create a new class instance

11. What does the following TypeScript code accomplish?

interface ClockInterface {
    currentTime: Date;
class Clock implements ClockInterface {
    currentTime: Date = new Date();
constructor(h: number, m: number) {}
a) Defines an interface and a class that initializes the current time
b) Creates a new instance of Date with the current time
c) Sets a timeout
d) Throws an error

12. How do you declare an array of only string types in TypeScript?

let fruits: string[];
a) let fruits: string[];
b) let fruits: array[string];
c) let fruits = ['Apple', 'Banana', 'Cherry'];
d) let fruits: Array(string);

13. Which keyword is used to declare a constant variable in TypeScript?

a) var
b) let
c) const
d) static

14. What is the result of the following TypeScript code?

let a: any = 'hello';
let b: string = a;
a) Outputs 'hello'
b) Throws a type error
c) Outputs undefined
d) Nothing, it's a syntax error

15. How can generics be used in TypeScript?

a) To create components that work with any data type
b) To specify that a function returns a string
c) To define the length of an array
d) To check types at runtime

16. What is a tuple in TypeScript?

a) An array with mixed data types
b) An array with a fixed number of elements with known types
c) A function that returns multiple values
d) A type that combines multiple classes

17. What is TypeScript's keyof operator used for?

interface Person {
    name: string;
    age: number;
type P = keyof Person;
a) To create a new type based on the keys of the Person
b) To retrieve the value of the name property from a Person object
c) To add new keys to the Person interface
d) To lock the Person interface from modifications

18. How is type inference used in TypeScript?

a) To explicitly set the type of variables
b) To check the type of a variable at runtime
c) To automatically detect and assign the most appropriate type
d) To output the types to the console during debugging

19. How can you use a class as a type in TypeScript?

class Point {
    x: number;
    y: number;
let p: Point;
a) By declaring it with the class keyword
b) By using it directly as a type
c) By creating an instance with new
d) By declaring it as type Point

20. What is the utility of the optional chaining operator in TypeScript?

let x = foo?.bar;
a) It checks if foo is null or undefined before accessing bar
b) It declares foo if it doesn't exist
c) It removes properties from foo dynamically
d) It ensures that foo is never null or undefined

21. What does the following TypeScript enum declaration signify?

enum Direction {
a) Creates a set of named constants for direction with auto-incremented values
b) Declares a new object type with properties Up, Down, Left, and Right
c) Defines a function that returns one of the directions
d) Sets a type that can be either Up, Down, Left, or Right

22. How do you annotate a function that takes a string and returns nothing in TypeScript?

function log(message: string): void {
a) function log(message: string): void
b) function log(message: string): undefined
c) function log(message: string): null
d) function log(message: string): string

23. What is the correct way to add a static property to a class in TypeScript?

class Circle {
    static pi: number = 3.14;
a) class Circle { static pi: number = 3.14; }
b) class Circle { const pi: number = 3.14; }
c) class Circle { let pi: number = 3.14; }
d) class Circle { pi: number = 3.14; }

24. How do you handle errors in TypeScript?

a) Using the try/catch/finally statements
b) Using the onError function
c) Using the catch operator on a type
d) TypeScript does not have built-in error handling

25. What is the purpose of TypeScript's this keyword in a class?

a) To refer to the class itself
b) To refer to the current instance of the class
c) To declare static methods
d) To create a new instance of the class