TypeScript Quiz
TypeScript adds static types to JavaScript to catch mistakes before code runs. The quiz covers basic types, interfaces, generics, enums, classes and how TypeScript compiles to plain JavaScript.
What this quiz covers
- Basic and union types
- Interfaces and type aliases
- Generics and enums
- Classes and compilation
Study tip: Turn on strict mode in a small project; the compiler messages teach you the type rules as you go.
Questions, answers and explanations
1. What is TypeScript?
- A JavaScript library
- A superset of JavaScript
- A CSS framework
- A database
Answer: A superset of JavaScript
TypeScript is a strongly typed superset of JavaScript developed by Microsoft.
2. Who developed TypeScript?
- Microsoft
- Oracle
Answer: Microsoft
TypeScript was developed and is maintained by Microsoft.
3. Which file extension is used for TypeScript files?
- .ts
- .js
- .tsxx
- .type
Answer: .ts
TypeScript source files use the .ts extension.
4. Which command compiles a TypeScript file?
- tsc
- ts
- compile
- npm compile
Answer: tsc
The TypeScript compiler is called using the 'tsc' command.
5. Which keyword is used to declare a variable in TypeScript?
- let
- var
- const
- All of the above
Answer: All of the above
TypeScript supports var, let, and const just like JavaScript.
6. Which symbol is used to specify a variable's type?
- :
- ;
- =
- #
Answer: :
A colon (:) is used to declare the data type of a variable.
7. Which of the following is a valid TypeScript type?
- number
- string
- boolean
- All of the above
Answer: All of the above
TypeScript includes built-in types such as number, string, and boolean.
8. Which keyword defines an interface in TypeScript?
- interface
- class
- type
- struct
Answer: interface
The interface keyword defines the structure of an object.
9. Which keyword is used to create a class in TypeScript?
- class
- object
- struct
- define
Answer: class
The class keyword creates a class in TypeScript.
10. Which TypeScript feature helps detect errors during development?
- Static Type Checking
- DOM
- Bootstrap
- CSS Modules
Answer: Static Type Checking
Static typing helps catch errors before the code runs.
11. Which keyword is used to create a custom type in TypeScript?
- type
- interface
- class
- define
Answer: type
The 'type' keyword creates type aliases in TypeScript.
12. Which TypeScript type can hold any kind of value?
- any
- mixed
- unknown
- object
Answer: any
The 'any' type disables type checking for a variable.
13. Which type is safer than 'any' because it requires type checking before use?
- unknown
- object
- dynamic
- void
Answer: unknown
The 'unknown' type requires type narrowing before operations.
14. Which keyword is used to inherit a class in TypeScript?
- extends
- inherits
- implements
- super
Answer: extends
The 'extends' keyword is used for class inheritance.
15. Which keyword is used to implement an interface?
- implements
- extends
- interface
- inherit
Answer: implements
Classes use the 'implements' keyword to implement interfaces.
16. Which type represents a function that returns no value?
- void
- null
- undefined
- never
Answer: void
The 'void' type indicates that a function does not return a value.
17. Which symbol is used for optional properties in TypeScript?
- ?
- !
- *
- &
Answer: ?
A question mark (?) marks a property as optional.
18. Which TypeScript feature allows one class to reuse another class's properties and methods?
- Inheritance
- Encapsulation
- Abstraction
- Polymorphism
Answer: Inheritance
Inheritance enables one class to reuse another class's functionality.
19. Which file is commonly used to configure TypeScript projects?
- tsconfig.json
- package.json
- config.ts
- typescript.json
Answer: tsconfig.json
The tsconfig.json file contains TypeScript compiler options.
20. What is the main advantage of using TypeScript?
- Static type checking
- Faster internet connection
- Smaller HTML files
- Automatic CSS generation
Answer: Static type checking
TypeScript catches many errors during development through static type checking.