TypeScript Project Planning & Execution 3 — Questions and Answers
Question 1: Which tool is commonly used to enforce consistent TypeScript code style and catch common bugs beyond what the compiler checks?
- Prettier
- ESLint with @typescript-eslint (Correct answer)
- Babel
- ts-node
Correct answer: ESLint with @typescript-eslint
The `@typescript-eslint` plugin extends ESLint with TypeScript-aware rules for linting patterns the compiler doesn't flag.
Question 2: In a TypeScript project, what is the purpose of a `.d.ts` declaration file?
- To run TypeScript code directly without compilation
- To provide type information for JavaScript libraries without source code (Correct answer)
- To configure the TypeScript compiler options
- To define environment variables for the build
Correct answer: To provide type information for JavaScript libraries without source code
Declaration files (`.d.ts`) expose type definitions for libraries so TypeScript consumers get autocomplete and type safety.
Question 3: Your team uses `strict: true` in tsconfig. Which individual flag does this NOT enable?
- strictNullChecks
- noImplicitAny
- noUnusedLocals (Correct answer)
- strictFunctionTypes
Correct answer: noUnusedLocals
`noUnusedLocals` is a separate flag not included in `strict: true`; it must be opted into independently.
Question 4: Which approach correctly types a React component's props in a TypeScript project to enforce required vs optional fields?
- Use a plain JavaScript object with default values
- Define an interface with optional properties using `?` and required ones without (Correct answer)
- Use `PropTypes` from the react package
- Pass `any` type to avoid type errors
Correct answer: Define an interface with optional properties using `?` and required ones without
TypeScript interfaces with `?` for optional properties and required fields without it give components precise type safety.
Question 5: What does enabling `isolatedModules: true` in tsconfig prevent, and why is it important for tools like Babel?
- It prevents circular imports between modules
- It disallows const enums and namespace imports that require full-program analysis (Correct answer)
- It blocks dynamic imports in the codebase
- It enforces a single entry point per project
Correct answer: It disallows const enums and namespace imports that require full-program analysis
`isolatedModules` disallows TypeScript features that Babel (which transpiles file-by-file) cannot handle without full-program context.
Question 6: A TypeScript project's build is slow. Which tsconfig option enables TypeScript to reuse information from previous compilations to speed up incremental builds?
- skipLibCheck: true
- incremental: true (Correct answer)
- noEmit: true
- composite: true
Correct answer: incremental: true
`incremental: true` saves a build info file (`.tsbuildinfo`) so TypeScript only recompiles changed files on subsequent builds.
Question 7: Which npm script pattern is best practice for running type checking and tests in parallel in a TypeScript project's CI pipeline?
- "ci": "tsc --noEmit && jest"
- "ci": "tsc --noEmit & jest"
- "ci": "npm-run-all --parallel typecheck test" (Correct answer)
- "ci": "jest --typescript"
Correct answer: "ci": "npm-run-all --parallel typecheck test"
`npm-run-all --parallel` runs multiple npm scripts concurrently, reducing total CI time compared to sequential execution.
Which tool is commonly used to enforce consistent TypeScript code style and catch common bugs beyond what the compiler checks?