Welcome to the TypeScript quiz for beginners! TypeScript has quickly become popular in modern web development because of its powerful type system on top of JavaScript. It’s time to test your knowledge of the basics of TypeScript! Here are 30+ multiple-choice questions to check your TypeScript foundational understanding.
Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. What is TypeScript primarily used for?
Answer:
Explanation:
TypeScript is primarily used to add static typing to JavaScript, enabling developers to catch type-related errors during compile time.
2. Which of the following is NOT a valid TypeScript data type?
Answer:
Explanation:
TypeScript does not have a "dynamic" type. It has the "any" type which can represent any JavaScript value.
3. How do you denote a variable as readonly in TypeScript?
Answer:
Explanation:
The readonly keyword in TypeScript ensures that a property cannot be re-assigned after its initial assignment.
4. How do you specify that a function does not return anything in TypeScript?
Answer:
Explanation:
In TypeScript, a function that doesn't return anything should have its return type specified as void.
5. How do you define a custom type in TypeScript?
Answer:
Explanation:
In TypeScript, custom types can be defined using both interface and type keywords.
6. What is the primary purpose of TypeScript interfaces?
Answer:
Explanation:
Interfaces in TypeScript are primarily used to describe the shape or structure of an object. They ensure that objects have the correct properties and methods as described by the interface.
7. What is a union type in TypeScript?
Answer:
Explanation:
A union type describes a value that could be one of several different types.
8. Which TypeScript feature allows for checking the type of a variable at runtime?
Answer:
Explanation:
Type guards allow you to narrow down the type of an object within a conditional block.
9. What TypeScript compiler option ensures strict type checking?
Answer:
Explanation:
The --strict compiler option enables a wide range of type-checking behavior to ensure that type definitions are accurate.
10. How do you define an optional parameter in the TypeScript function?
Answer:
Explanation:
In TypeScript, you can define an optional parameter by appending a '?' to the parameter name.
11. Which of the following will transpile a TypeScript file (example.ts) to JavaScript?
Answer:
Explanation:
The tsc (TypeScript Compiler) command is used to transpile TypeScript files to JavaScript.
12. How do you declare a variable that can be either a string or null in TypeScript?
Answer:
Explanation:
The union type (using '|') allows a variable to have one of multiple types.
13. What is the purpose of the never type in TypeScript?
Answer:
Explanation:
The never type represents the type of values that never occur. For instance, a function that always throws an exception or one that never returns.
14. How can you allow an object to have any number of properties of a given type in TypeScript?
Answer:
Explanation:
This syntax denotes an object that can have any number of properties, where the keys are strings and the values can be of any type.
15. Which command would you use to install TypeScript globally using npm?
Answer:
Explanation:
The -g flag in npm is used to install packages globally.
16. How do you define private property in a TypeScript class?
Answer:
Explanation:
In TypeScript, the private keyword is used to define private properties in a class.
17. Which of the following TypeScript types can the unknown type be assigned to without type assertion?
Answer:
Explanation:
The unknown type is a safe counterpart of any type. It can be assigned to any type without a type assertion.
18. In TypeScript, what does an enum allow you to do?
Answer:
Explanation:
Enums in TypeScript allow for a way of giving more friendly names to sets of numeric or string values.
19. Which TypeScript feature allows for declaring new names for existing types?
Answer:
Explanation:
Type aliases allow for creating new names (aliases) for types. It's a way to give a type a new name.
20. How do you specify a function type in TypeScript that takes in a number and returns a string?
Answer:
Explanation:
In TypeScript, function types can be described using the parameter type and return type syntax, such as (parameterType) => returnType.
21. What does the extends keyword allow you to do in TypeScript?
Answer:
Explanation:
In TypeScript, as in many object-oriented languages, the extends keyword is used for class declarations or class expressions to create a class as a child of another class.
22. Which TypeScript keyword allows for a child class to override a method of its parent class?
Answer:
Explanation:
The super keyword in TypeScript is used to call functions on an object's parent. It is often used within an overridden method to refer to the version of the function in the parent class.
23. How do you define an array of strings in TypeScript?
Answer:
Explanation:
In TypeScript, both Array<string> and string[] are valid ways to define an array of strings.
24. In TypeScript, how do you enforce a variable to be of a specific type at compile time?
Answer:
Explanation:
Type annotations in TypeScript allow developers to enforce a variable to be of a specific type at compile time.
25. Which TypeScript feature provides static typings for dynamic properties in objects and arrays?
Answer:
Explanation:
Index signatures in TypeScript allow us to describe types of properties that might not be known until runtime.
26. In TypeScript, how can a subclass access a method from its superclass?
Answer:
Explanation:
The super keyword in TypeScript is used to call functions on an object's parent. When overriding a method in a derived class, the super keyword can be used to call the method on the parent class.
27. How do you declare a class in TypeScript?
Answer:
Explanation:
In TypeScript, you use the class keyword followed by the class name to define a new class, similar to ES6 in JavaScript.
28. How do you create an instance of a TypeScript class?
Answer:
Explanation:
You create a new instance of a TypeScript class using the new keyword followed by the class name and parentheses.
29. What does the extends keyword do in TypeScript?
Answer:
Explanation:
The extends keyword is used for class inheritance in TypeScript. It allows a class to inherit methods and properties from another class.
30. What is the purpose of a constructor in TypeScript classes?
Answer:
Explanation:
The constructor in TypeScript is a special function that is automatically called when an object is created from a class. It is primarily used to initialize object properties.
Comments
Post a Comment
Leave Comment