Test your understanding of fundamental TypeScript concepts with these carefully selected questions. This quiz covers TypeScript static typing, interfaces, enums, generics, and other key TypeScript features to help you prepare for interviews.
Understanding TypeScript
Which statement best describes TypeScript?
- TypeScript is a database management tool.
- TypeScript is a runtime environment for JavaScript.
- TypeScript is only used for backend development.
- TypeScript is a statically typed superset of JavaScript.
Typing in TypeScript
How do you explicitly declare a variable 'count' as a number in TypeScript?
- let count: number = 10;
- let count = number;
- var count: 10 = number;
- number count = 10;
TypeScript vs JavaScript
What is a primary difference between TypeScript and JavaScript?
- JavaScript supports interfaces natively.
- JavaScript supports static typing while TypeScript does not.
- TypeScript allows declaring variable types at compile-time.
- TypeScript cannot be used for client-side applications.
Interfaces Usage
How does an interface help in TypeScript? For example: interface User { name: string; age: number; }
- It is used to connect to databases.
- It defines the structure of an object for type-checking.
- It provides a way to create UI components.
- It creates new HTML elements.
Enums in TypeScript
What is the purpose of enums in TypeScript? Example: enum Direction { Up, Down }
- To store lists of objects
- To dynamically change variable types
- To define named constant values for easier code readability
- To declare functions inside classes
Working with Tuples
Which of the following is a correct tuple declaration in TypeScript?
- tuple pair = [42, 'Answer'];
- let pair = [number, string] = [42, 'Answer'];
- let pair: [number, string] = [42, 'Answer'];
- let pair: tuple = [42, 'Answer'];
Understanding the 'any' Type
Which statement about the 'any' type in TypeScript is correct?
- 'any' only works in interfaces.
- 'any' type disables type checking for the assigned variable.
- 'any' prevents variables from holding string values.
- 'any' type forces all variables to be numbers.
Using Generics
What is the benefit of using generics in TypeScript functions? Example: function identityu003CTu003E(value: T): T { return value; }
- It forces all data types to be strings.
- It makes variables global in scope.
- It allows functions to work with multiple types while maintaining type safety.
- It disables all type checks.
Classes in TypeScript
Which keyword is used to create a subclass that inherits from another class in TypeScript?
- implement
- extends
- derives
- inherits
Type Inference Concept
What does type inference mean in TypeScript? For example: let flag = true;
- TypeScript forces a variable to have no type.
- TypeScript automatically assigns a type to a variable based on its initial value.
- TypeScript ignores variable values.
- TypeScript only allows defining variable types manually.