---
title: "TypeScript: Typed JavaScript Fundamentals"
description: "Test your knowledge of TypeScript fundamentals with this comprehensive quiz covering static typing, interfaces, generics, type guards, enums, classes, advanced types, and TypeScript best practices."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/quizzes/post/typescript-fundamentals-quiz
---

# TypeScript: Typed JavaScript Fundamentals

Welcome to the TypeScript Basics Quiz! This quiz is designed to test your understanding of fundamental TypeScript concepts, static typing, and type-safe programming best practices. Each question is multiple-choice, and you'll find hints to help you along the way. Good luck!

## Questions

### 1. What is the primary purpose of TypeScript?

- To increase the execution speed of the browser engine
  - TypeScript is transpiled to JavaScript; it does not change the speed of the underlying runtime engine.
- **To provide static type checking for JavaScript code** ✅
  - TypeScript adds a layer of type safety that catches potential errors during the compilation phase.
- To eliminate the need for CSS and HTML in development
  - TypeScript is a superset of JavaScript; it does not replace the markup or styling layers of the web.
- To automate the management of remote database servers
  - While TypeScript is used in back-end logic, its core mission is type safety, not server orchestration.

**Hint:** It adds something JavaScript is missing.

### 2. Which command is used to compile a TypeScript file into JavaScript?

- `npm start`
  - This command typically executes a script defined in package.json rather than invoking the compiler directly.
- **`tsc`** ✅
  - The tsc (TypeScript Compiler) utility is the standard tool for transforming .ts files into executable .js files.
- `node --compile`
  - Node.js is a runtime environment that executes JavaScript; it does not include a native TypeScript compiler.
- `ts-build`
  - While descriptive, ts-build is not the name of the standard binary provided by the TypeScript package.

**Hint:** TypeScript Compiler.

### 3. How do you define an interface for a User object?

- `type User = { name: string; }`
  - This syntax defines a Type Alias, which is functionally similar but distinct from a formal Interface.
- **`interface User { name: string; }`** ✅
  - This is the standard syntax for defining an interface to enforce a specific shape on an object.
- `class User { name: string; }`
  - A class creates a concrete blueprint with runtime presence, whereas an interface is a compile-time construct.
- `struct User { name: string; }`
  - The struct keyword is common in languages like C# or C++, but it is not valid syntax in TypeScript.

**Hint:** Use the "interface" keyword.

### 4. What does the "any" type do?

- It restricts a variable to only holding string data
  - Restricting a variable to strings is the role of the "string" type, not the "any" type.
- **It disables type checking for a particular variable** ✅
  - The any type allows a variable to bypass the type checker, effectively treating it as plain JavaScript.
- It allows a variable to be assigned only a null value
  - To restrict a variable to null, you would use the "null" type or a union including null.
- It creates a constant that cannot be changed later
  - Immutability is handled by the "const" keyword, not by the type system assignment of "any".

**Hint:** It turns off the safety features.

### 5. What is a "Tuple" in TypeScript?

- An array that automatically grows as items are added
  - Standard JavaScript arrays are dynamic; Tuples are defined by a specific, fixed size.
- **An array with a fixed number of elements and known types** ✅
  - Tuples allow you to express an array where the type of a fixed number of elements is known in advance.
- A function that returns two different data types at once
  - While a function can return a tuple, the term "Tuple" refers to the data structure itself.
- A variable that can hold a string or a number selectively
  - This describes a Union Type, which allows for multiple types but does not enforce array positioning.

**Hint:** A list with a fixed number of elements and types.

### 6. Which keyword is used to make an interface property optional?

- `!`
  - The exclamation mark is the non-null assertion operator, used to tell the compiler a value is present.
- **`?`** ✅
  - Appending a question mark to a property name indicates that the property is not required for a valid object.
- `void`
  - Void is used to indicate that a function does not return a value, not to mark properties as optional.
- `null`
  - The null keyword represents an intentional absence of value, but it does not make a property optional.

**Hint:** The punctuation mark for a question.

### 7. What is an "Enum"?

- A built-in method for encrypting sensitive string data
  - Enums are used for code organization and readability, not for security or cryptographic operations.
- **A collection of related and named constant numeric values** ✅
  - Enums allow developers to define a set of named constants, making it easier to document intent.
- A specific type of loop used to iterate through objects
  - Iteration is handled by loops like for-in or for-of; an Enum is a custom data type.
- A function that executes itself immediately upon creation
  - Self-executing functions are a logical pattern (IIFE), not a data structure like an Enum.

**Hint:** A set of named constants.

### 8. What does the "void" type signify when used on a function?

- The function will terminate the program with an error
  - The return type for a function that always throws or fails to finish is "never", not "void".
- **The function performs an action but returns no value** ✅
  - Void is the return type of functions that do not return a value to the caller after execution.
- The function body has been left empty by the developer
  - A function can contain significant logic and still be "void" as long as it lacks a return statement.
- The function is only accessible within its current class
  - Access control is managed by the "private" or "protected" modifiers, not the "void" return type.

**Hint:** The function does its job but gives nothing back.

### 9. How do you define a Union Type in TypeScript?

- `string & number`
  - The ampersand creates an Intersection Type, which requires a value to meet all combined requirements.
- **`string | number`** ✅
  - The pipe symbol creates a Union Type, allowing a variable to satisfy any one of the listed types.
- `string + number`
  - The plus symbol is used for arithmetic or string concatenation and is not valid for type definitions.
- `string [] number`
  - Square brackets are used to define array types and do not facilitate the creation of union types.

**Hint:** The "pipe" symbol.

### 10. What is the "unknown" type?

- A placeholder for variables that will be soon deprecated
  - Deprecation is typically handled via documentation tags like @deprecated, not via the type system.
- **A safer type that requires a type check before any usage** ✅
  - The unknown type is a safer alternative to any because it prevents you from calling methods on it without a check.
- A type used to represent variables that have been deleted
  - Memory management and deletion are handled by the runtime; there is no specific type for deleted data.
- A specialized type for handling error objects in catches
  - While "unknown" is often used in catch blocks, its purpose is general-purpose type safety.

**Hint:** A safer version of "any".

### 11. What is the purpose of the "readonly" keyword?

- To ensure a property cannot be accessed by outside files
  - Access restriction is the role of the "private" keyword, whereas readonly controls modification.
- **To prevent a property from being changed after assignment** ✅
  - Readonly makes a property immutable after its initial initialization in a class or an interface.
- To optimize the loading speed of large data structures
  - Readonly is a compile-time safety feature and does not provide inherent hardware performance gains.
- To mark a variable for inclusion in the browser storage
  - Browser storage (like localStorage) is managed via Web APIs, not through TypeScript type keywords.

**Hint:** It prevents changes.

### 12. What is "Generics" in TypeScript?

- A library of pre-written functions for common web tasks
  - Generics are a language feature, not a collection of utility functions or external libraries.
- **A way to create components that work with a variety of types** ✅
  - Generics allow you to create reusable code that can adapt to different types while maintaining safety.
- A tool for generating random data for testing applications
  - While you could use generics in a generator, the term refers to the type-parameterization feature.
- A method for translating TypeScript into different languages
  - Translation or transpilation is handled by the tsc compiler, not by the generics system.

**Hint:** Creating components that work over a variety of types.

### 13. Which file contains the configuration for a TypeScript project?

- `package.json`
  - This file manages project metadata and dependencies, but not specific TypeScript compiler options.
- **`tsconfig.json`** ✅
  - This file is the root configuration where you define compiler options and file inclusion rules.
- `typescript.json`
  - TypeScript specifically looks for the tsconfig.json filename to resolve its project settings.
- `.ts-settings`
  - Configuration files with a dot-prefix are common in other tools, but TypeScript uses tsconfig.json.

**Hint:** ts-config.

### 14. What is "Type Inference"?

- When a developer explicitly defines a variable as a string
  - Explicitly defining the type is called Type Annotation, which is the opposite of Inference.
- **When the compiler automatically detects a variable type** ✅
  - Type Inference occurs when TypeScript determines the type of a value based on how it is initialized.
- A technique for forcing one data type into another type
  - Forcing a type change is known as Type Casting or Type Assertion, not Type Inference.
- A process for removing all type information from a file
  - Type removal happens during the transpilation/erasure phase when converting TS code to JS.

**Hint:** TypeScript guessing the type.

### 15. What is the "never" type used for?

- For variables that are initialized with a null value
  - Variables initialized as null use the "null" type, as "never" implies a state that is unreachable.
- **For functions that do not reach an end point or return** ✅
  - The never type represents values that will never occur, such as the return of a function that always throws.
- For components that are not intended to be reused
  - Reusability is a design choice; the type system uses "never" for control-flow analysis.
- For code that is executed only during the testing phase
  - Test-only code is managed via build configurations or file exclusions, not via the "never" type.

**Hint:** For values that will never occur.

### 16. How do you extend an interface?

- `interface Admin + User`
  - The plus operator is not used for type inheritance or extending interfaces in TypeScript.
- **`interface Admin extends User`** ✅
  - The extends keyword allows an interface to inherit the properties and methods of another interface.
- `interface Admin : User`
  - The colon syntax is used in languages like C#, but TypeScript uses the extends keyword.
- `interface Admin is User`
  - The "is" keyword is used for type guards in functions, not for extending interface definitions.

**Hint:** Use the "extends" keyword.

### 17. What is an "Abstract Class"?

- A class that is hidden from other modules in the project
  - Visibility is controlled by access modifiers; an abstract class is visible but cannot be instantiated.
- **A base class that cannot be instantiated directly** ✅
  - Abstract classes act as blueprints for other classes and must be extended to be used.
- A class that only contains static methods and constants
  - A class with only static members is often called a Utility Class, not necessarily an Abstract Class.
- A class designed to represent visual shapes in a UI
  - While "Shape" might be a common example, "Abstract" refers to the class logic, not the subject matter.

**Hint:** A class that cannot be instantiated on its own.

### 18. Which keyword is used to access the parent class constructor?

- `parent()`
  - The parent keyword is common in PHP, but JavaScript and TypeScript use the super keyword.
- **`super()`** ✅
  - The super keyword is used to call the constructor of the base class within a derived class.
- `base()`
  - The base keyword is used in C#; TypeScript uses super to interact with the parent class.
- `this.constructor()`
  - Calling the constructor via this refers to the current class, not specifically the parent class.

**Hint:** Think "Super".

### 19. What is a "Type Guard"?

- A security mechanism that prevents unauthorized code access
  - Type guards are for type-safety and logic flow, not for user authentication or security.
- **A check that narrows a type within a specific scope** ✅
  - Type guards use runtime checks (like typeof) to narrow down a more general type to a specific one.
- A function that validates that a user is logged in
  - Validating sessions is an application logic task, while Type Guards are a compiler safety feature.
- A variable that stores the current state of an interface
  - State is stored in standard variables; a guard is a conditional logic pattern for the compiler.

**Hint:** It narrows down a type within a block of code.

### 20. What is the "as" keyword used for?

- `To rename a variable for use in a different file`
  - Renaming variables during export/import uses "as", but as a type operator, it has a different role.
- **`To tell the compiler to treat a value as a specific type`** ✅
  - This is called Type Assertion; it is used when you have more specific info than the compiler.
- `To import a specific module into the global scope`
  - Imports use the import keyword; "as" is only used within that statement for aliasing.
- `To create a loop that iterates through an array`
  - Iteration is handled by keywords like for-of, not by the "as" type assertion keyword.

**Hint:** Type Assertion.

### 21. What is the result of transpiling TypeScript to JavaScript?

- A compressed file that executes faster in the browser
  - Transpilation and minification are different steps; transpilation alone does not increase speed.
- **A standard JavaScript file with all types removed** ✅
  - The compiler removes all TypeScript-specific syntax, resulting in standard JavaScript code.
- An encrypted file that is unreadable to human developers
  - The output is standard JavaScript text, which remains human-readable unless minified.
- A binary file that runs directly on the server hardware
  - JavaScript is an interpreted/JIT language; it does not compile into machine-level binary code.

**Hint:** The types disappear.

### 22. How do you declare a private property in a TypeScript class?

- `hidden name: string;`
  - The "hidden" keyword is not a valid access modifier in the TypeScript language specification.
- **`private name: string;`** ✅
  - The private modifier ensures the property is only accessible from within the class itself.
- `#name: string;`
  - While this is a private field in ES6, the question asks for the TypeScript-specific keyword.
- `internal name: string;`
  - The "internal" keyword is used in C# or Swift, but it is not a modifier in TypeScript.

**Hint:** The "private" keyword.

### 23. What is "Literal Types"?

- A type that allows any string or number to be stored
  - This describes general types like string or number, not the more specific Literal types.
- **A type that can only hold a specific, exact value** ✅
  - Literal types allow you to specify exact values (like "Success") that a variable must match.
- A type used for documentation in large-scale projects
  - Documentation is handled by comments; Literal types are enforced by the compiler logic.
- A type that can be changed by the user at runtime
  - Types are erased at runtime; Literal types only exist to provide safety during development.

**Hint:** The type is a specific value.

### 24. What is "Intersection Type"?

- A type that can be one of several different types
  - This describes a Union Type (A | B), not an Intersection Type (A & B).
- **A type that combines multiple types into a single one** ✅
  - An Intersection Type creates a new type that has all the properties of the combined types.
- A type that is used to connect two separate databases
  - Databases are external systems and are not part of the internal TypeScript type system.
- A type that prevents a function from being executed
  - Functions are controlled by logic; types are used for validating the data those functions use.

**Hint:** Combining multiple types into one.

### 25. What is the "protected" modifier?

- It makes the property accessible to everyone globally
  - Global accessibility is the role of the "public" modifier, the most permissive setting.
- **It makes properties visible to a class and its children** ✅
  - The protected modifier allows subclasses to access members while keeping them private to others.
- It encrypts the property so it cannot be viewed in JS
  - Modifiers are a compile-time visibility tool and do not provide runtime data encryption.
- It prevents the property from being deleted or removed
  - Deletion is a runtime operation in JavaScript; "protected" only manages access visibility.

**Hint:** Visible to the family.

### 26. How do you define a function type in an interface?

- `myFunc: void;`
  - This syntax defines a property named myFunc with a type of void, not a callable function.
- **`myFunc(): void;`** ✅
  - This defines a method within an interface, specifying both the name and the return type.
- `function myFunc(): void;`
  - The "function" keyword is not used when defining methods within an interface block.
- `def myFunc(): void;`
  - The "def" keyword is used in Python or Ruby and is not a valid TypeScript keyword.

**Hint:** name(params): returnType

### 27. What is "Namespace" in TypeScript?

- A way to organize files within the operating system
  - File organization is part of the file system; Namespaces are an internal code grouping tool.
- **A way to group related code and prevent collisions** ✅
  - Namespaces help organize code and prevent names from clashing in the global scope.
- The specific URL where your project is hosted online
  - A hosting URL is a network address and is not related to the TypeScript Namespace keyword.
- A specialized database for storing key-value pairs
  - Key-value storage is a database task; Namespaces are for logical grouping of types.

**Hint:** Grouping related code.

### 28. What is the "keyof" operator?

- It checks if a specific key is present in an object
  - Checking for presence is done at runtime with the "in" operator, not the "keyof" type operator.
- **It creates a union type of all keys from an object type** ✅
  - The keyof operator takes an object type and produces a union of its property names.
- It renames the primary key of a database table record
  - TypeScript does not interact with database schemas; it only manages local code types.
- It unlocks a variable that was marked as private
  - Privacy modifiers cannot be bypassed by operators; they are enforced by the compiler.

**Hint:** It extracts the keys.

### 29. What does "Strict Mode" in tsconfig do?

- It prevents the code from running on mobile browsers
  - Strict mode is a compiler safety setting and does not affect browser compatibility.
- **It enables comprehensive checks for more robust code** ✅
  - Strict mode enables a suite of checks, such as null checks, to ensure higher code quality.
- It forces all variable names to be written in all caps
  - Naming conventions are a style choice (linting), not a strict mode compiler requirement.
- It automatically deletes code that contains any errors
  - The compiler will prevent a build if errors exist, but it will never delete your source code.

**Hint:** It makes the compiler very picky.

### 30. What is the "!" operator (Non-null assertion)?

- It indicates that a variable is not used in the file
  - Unused variables are typically flagged by the compiler, not marked with an exclamation point.
- **It asserts that a value is not null or undefined** ✅
  - The non-null assertion operator tells the compiler to ignore null/undefined checks for that variable.
- It triggers an immediate error if a variable is empty
  - This operator is a compile-time hint to *avoid* errors; it does not perform runtime checks.
- It makes a variable available to all other functions
  - Variable availability is determined by scope and modifiers, not by the assertion operator.

**Hint:** You are telling the compiler: "Trust me, it exists".
