JavaScript TypeScript Basics 4 — Questions and Answers
Question 1: What is a discriminated union in TypeScript?
- A union where each member has a common literal property used to narrow the type (Correct answer)
- A union type that excludes null and undefined
- A union that only allows primitive types
- A union created using the `discriminate` keyword
Correct answer: A union where each member has a common literal property used to narrow the type
Discriminated unions use a shared literal property (discriminant) to narrow which union member is in use inside conditional blocks.
Question 2: What does enabling `strict` mode in `tsconfig.json` do?
- Enables only `strictNullChecks`
- Enables a set of strict type-checking options including `strictNullChecks`, `noImplicitAny`, and others (Correct answer)
- Disables all optional type checks
- Locks the TypeScript version
Correct answer: Enables a set of strict type-checking options including `strictNullChecks`, `noImplicitAny`, and others
The `strict` flag is a shorthand that enables a broad set of type safety options simultaneously.
Question 3: What is the output of TypeScript compilation?
- A binary executable
- JavaScript files (Correct answer)
- WebAssembly modules
- JSON configuration files
Correct answer: JavaScript files
TypeScript compiles down to plain JavaScript files that can run in any JavaScript environment.
Question 4: Which of the following correctly uses an intersection type in TypeScript?
- type AB = A & B; (Correct answer)
- type AB = A | B;
- type AB = A + B;
- type AB = A extends B;
Correct answer: type AB = A & B;
The `&` operator creates an intersection type, combining all properties of both types into one.
Question 5: What does the `?. ` (optional chaining) operator do in TypeScript?
- Creates an optional property in a type definition
- Safely accesses a property, returning undefined instead of throwing if the object is null or undefined (Correct answer)
- Declares a variable as possibly undefined
- Checks if a variable is null before assignment
Correct answer: Safely accesses a property, returning undefined instead of throwing if the object is null or undefined
Optional chaining (`?.`) short-circuits to `undefined` if the left-hand side is null or undefined, preventing runtime errors.
Question 6: What is the purpose of declaration files (`.d.ts`) in TypeScript?
- They contain compiled JavaScript output
- They provide type information for JavaScript libraries without including implementation code (Correct answer)
- They define database schemas for TypeScript projects
- They store environment variable declarations
Correct answer: They provide type information for JavaScript libraries without including implementation code
`.d.ts` files are pure type declarations that let TypeScript understand the shape of external JavaScript code.
Question 7: Which TypeScript feature allows you to define a type based on the return type of a function?
- typeof
- keyof
- ReturnType<T> (Correct answer)
- InstanceType<T>
Correct answer: ReturnType<T>
`ReturnType<T>` is a utility type that extracts the return type of a function type T.
What is a discriminated union in TypeScript?