TypeScript Online Quiz

Embark on a journey to test your TypeScript knowledge with our comprehensive online quiz! TypeScript, a superset of JavaScript, adds static types to the language, enhancing code quality and maintainability. This quiz spans a variety of topics, from basic type annotations and interfaces to advanced concepts like generics and decorators. Whether you are new to TypeScript or looking to deepen your understanding, these questions are designed to challenge and refine your TypeScript skills.

1. What is TypeScript primarily used for?

a) Adding HTML templates to JavaScript
b) Adding static typing to JavaScript
c) Speeding up JavaScript runtime
d) Compiling JavaScript into machine code

2. What will the following TypeScript code output?

let isDone: boolean = true;
a) "true"
b) "false"
c) true
d) false

3. How do you declare a variable whose type is explicitly any in TypeScript?

a) let variable: any;
b) var variable: any;
c) let variable = any;
d) Any variable;

4. Which TypeScript feature allows you to work with parameters as an array of values?

a) Generics
b) Rest parameters
c) Array types
d) Tuples

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

function multiply(a: number, b: number): number {
    return a * b;
console.log(multiply(2, 3));
a) 6
b) "6"
c) null
d) undefined

6. In TypeScript, what is the purpose of an interface?

a) To define a type shape that an object can implement
b) To provide an implementation for classes
c) To initialize new objects
d) To declare standard methods

7. What will the following TypeScript code output?

let names: Array<string> = ['John', 'Jane', 'Joe'];
a) John
b) Jane
c) Joe
d) undefined

8. What is the correct way to declare a tuple in TypeScript?

a) let x: tuple = [10, "hello"];
b) let x: [number, string] = [10, "hello"];
c) let x: [10, "hello"];
d) let x = tuple[10, "hello"];

9. How do you define a function that accepts a string or a number as its parameter in TypeScript?

function logMessage(message: string | number): void {
logMessage("Hello, TypeScript!");
a) Using a union type
b) Using a generic type
c) Using an any type
d) Using a multiple type

10. What does the readonly keyword do when used in a TypeScript interface?

a) Makes the property mandatory to implement
b) Prevents the property from being changed once it is set
c) Makes the property private
d) Indicates the property can be left uninitialized

11. Which feature in TypeScript provides a way for better function composition by allowing functions to return functions?

a) Callbacks
b) Classes
c) Closures
d) Constants

12. What is the correct syntax to create a new class instance in TypeScript?

a) let obj = new Class();
b) let obj = Class.new();
c) let obj = constructor Class();
d) let obj = create Class();

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

a) It is used to declare the type of a variable
b) It is used to perform a type assertion
c) It is used to define an alias for a type
d) It is used to specify the return type of a function

14. What will the following TypeScript code output?

let x: any = "hello";
let y: number = (<string>x).length;
a) 5
b) "hello"
c) undefined
d) Error

15. How do you enable strict null checks in a TypeScript project?

a) Add "strictNullChecks": true to the tsconfig.json file
b) Use the command line flag --strictNullChecks
c) Both a) and b) are correct
d) It is enabled by default

16. Which TypeScript feature helps to ensure that all possible cases are handled when using union types?

a) Type guards
b) Discriminated unions
c) Exhaustiveness checking
d) Type narrowing

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

type Point = {
    x: number,
    y: number
    let p: Point = { x: 10, y: 20 };
    console.log(p.x + p.y);
a) 30
b) {x:10, y:20}
c) "1020"
d) None of the above

18. How do you annotate a method in a TypeScript class as an asynchronous function?

a) async function methodName() {}
b) async methodName() {}
c) methodName() async {}
d) function async methodName() {}

19. What will the following TypeScript code output?

enum Color {Red, Green, Blue}
let c: Color = Color.Green;
a) "Green"
b) 1
c) 0
d) 2

20. How do you declare a variable whose type can be either number or null in TypeScript?

a) let num: number | null;
b) let num: nullable<number>;
c) let num = number | null;
d) let num: null<number>;

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

interface Printable {
    print(): void;
let doc: Printable = {
print() { console.log("Hello, TypeScript!"); }
a) "Hello, TypeScript!"
b) "Printable"
c) undefined
d) Error

22. What does the never type represent in TypeScript?

a) A type that never has any value
b) A type used when a function never returns
c) A type that is unknown during development
d) Both a) and b) are correct

23. How do you specify that a class property is optional in TypeScript?

a) propertyName?
b) optional propertyName
c) propertyName optional
d) ?propertyName

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

function add(a: number, b: number = 5): number {
    return a + b;
a) 3
b) 5
c) 8
d) Error

25. What is TypeScript's equivalent for JavaScript's typeof operator for type guarding?

a) typeof
b) instanceOf
c) isTypeOf
d) type