TypeScript Project Planning & Execution 4 — Questions and Answers
Question 1: Your TypeScript library is published to npm. Which tsconfig option generates `.d.ts` files so consumers get type information?
- noEmit: true
- declaration: true (Correct answer)
- emitDecoratorMetadata: true
- outFile: true
Correct answer: declaration: true
`declaration: true` instructs TypeScript to emit `.d.ts` files alongside compiled JavaScript, providing type info to library consumers.
Question 2: Which versioning strategy should a TypeScript project library follow when making breaking changes to exported types?
- Patch version bump (1.0.0 → 1.0.1)
- Minor version bump (1.0.0 → 1.1.0)
- Major version bump (1.0.0 → 2.0.0) (Correct answer)
- Pre-release tag only (1.0.0 → 1.0.0-beta)
Correct answer: Major version bump (1.0.0 → 2.0.0)
Breaking changes to exported types are breaking API changes and require a major semver bump to signal incompatibility.
Question 3: In a TypeScript project, what is the role of `husky` and `lint-staged` in the development workflow?
- They compile TypeScript faster using caching
- They run linting and type checks only on staged files before each commit (Correct answer)
- They manage TypeScript version upgrades automatically
- They generate `.d.ts` files for JavaScript dependencies
Correct answer: They run linting and type checks only on staged files before each commit
`husky` sets up Git hooks and `lint-staged` runs scripts (lint, format, type check) only on staged files to keep commits clean.
Question 4: When planning a TypeScript migration from JavaScript, which flag helps you gradually add types by only erroring on explicitly typed code?
- checkJs: true
- strict: false
- allowJs: true with noImplicitAny: false (Correct answer)
- skipLibCheck: true
Correct answer: allowJs: true with noImplicitAny: false
Using `allowJs: true` with `noImplicitAny: false` lets JS files coexist with TypeScript without requiring all types immediately.
Question 5: Which TypeScript compiler option must be enabled to use experimental decorators (e.g., for frameworks like NestJS)?
- useDefineForClassFields
- experimentalDecorators (Correct answer)
- emitDecoratorMetadata
- strictPropertyInitialization
Correct answer: experimentalDecorators
`experimentalDecorators: true` enables the Stage 2 decorator proposal syntax used by frameworks like NestJS and Angular.
Question 6: A project uses `ts-jest` to run Jest tests on TypeScript files. What is the primary advantage over compiling TypeScript before running tests?
- It runs tests faster by skipping type checking entirely
- It enables in-process TypeScript compilation with source map support and no separate build step (Correct answer)
- It supports only ECMAScript modules
- It automatically generates test files from TypeScript interfaces
Correct answer: It enables in-process TypeScript compilation with source map support and no separate build step
`ts-jest` transforms TypeScript during test execution, preserving source maps for accurate error locations without a pre-compile step.
Question 7: Which `tsconfig.json` `module` setting is required for Node.js 18+ projects that use native ES module imports with `.js` extensions?
- CommonJS
- ES2015
- NodeNext (Correct answer)
- UMD
Correct answer: NodeNext
`"module": "NodeNext"` aligns TypeScript's module resolution with Node.js's native ESM behavior, including requiring `.js` extensions in imports.
Your TypeScript library is published to npm.
Which tsconfig option generates `.d.ts` files so consumers get type information?