Complete the code to import the TypeScript compiler API.
import * as ts from '[1]';
The TypeScript compiler API is imported from the 'typescript' package.
Complete the code to create a TypeScript program from source files.
const program = ts.createProgram({ rootNames: ['file.ts'], [1]: {} });The property to specify compiler options is 'compilerOptions'.
Fix the error in the code to get the source file from the program.
const sourceFile = program.getSourceFile([1]);The method getSourceFile expects the file name as a string.
Fill both blanks to create a printer and print the source file.
const printer = ts.createPrinter([1]); const result = printer.printFile([2]);
The printer can be created with options like removing comments, and printFile needs the source file object.
Fill all three blanks to create a diagnostic reporter and report errors.
const diagnostics = program.getSemanticDiagnostics();
diagnostics.forEach(diagnostic => {
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, [1]);
console.[2](`Error in file: ${diagnostic.file?.fileName} - ${message}`);
if (diagnostic.start !== undefined) {
const { line, character } = diagnostic.file!.getLineAndCharacterOfPosition([3]);
console.log(`At line ${line + 1}, character ${character + 1}`);
}
});The flattenDiagnosticMessageText needs a newline character, console.error is used for errors, and getLineAndCharacterOfPosition needs the diagnostic start position.