0
0
Typescriptprogramming~20 mins

TypeScript compiler API basics - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Compiler API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript compiler API code?

Consider the following TypeScript code that uses the compiler API to create a source file and print its text.

import ts from 'typescript';

const source = ts.createSourceFile('test.ts', 'const x = 42;', ts.ScriptTarget.Latest, true);
console.log(source.text);

What will be printed to the console?

Typescript
import ts from 'typescript';

const source = ts.createSourceFile('test.ts', 'const x = 42;', ts.ScriptTarget.Latest, true);
console.log(source.text);
AAn empty string
BSyntaxError
Cconst x = 42;
Dundefined
Attempts:
2 left
💡 Hint

Think about what source.text represents in a source file object.

🧠 Conceptual
intermediate
1:30remaining
Which TypeScript compiler API method creates a program from source files?

You want to analyze multiple TypeScript files together. Which method from the compiler API should you use to create a program that represents all these files?

Ats.transpileModule
Bts.createSourceFile
Cts.createPrinter
Dts.createProgram
Attempts:
2 left
💡 Hint

Think about which method handles multiple files and type checking.

🔧 Debug
advanced
2:30remaining
What error does this TypeScript compiler API code produce?

Examine this code snippet:

import ts from 'typescript';

const source = ts.createSourceFile('test.ts', 'let a = 1;', ts.ScriptTarget.Latest, true);
const printer = ts.createPrinter();

const result = printer.printNode(ts.EmitHint.Unspecified, source, source);
console.log(result);

What error or output will this code produce?

Typescript
import ts from 'typescript';

const source = ts.createSourceFile('test.ts', 'let a = 1;', ts.ScriptTarget.Latest, true);
const printer = ts.createPrinter();

const result = printer.printNode(ts.EmitHint.Unspecified, source, source);
console.log(result);
ATypeError: printNode expects a Statement node, not SourceFile
BPrints the entire source code: 'let a = 1;'
CSyntaxError: Argument of type 'SourceFile' is not assignable to parameter of type 'Node'
DPrints an empty string
Attempts:
2 left
💡 Hint

Check what printNode accepts as arguments and what SourceFile is.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a TypeScript source file with strict mode enabled?

You want to create a source file with the text "let y = 10;" and specify the script target as ES2020 with strict mode enabled. Which code snippet is correct?

Ats.createSourceFile('file.ts', 'let y = 10;', ts.ScriptTarget.ES2020, true);
Bts.createSourceFile('file.ts', 'let y = 10;', ts.ScriptTarget.ES2020, strict=true);
Cts.createSourceFile('file.ts', 'let y = 10;', ts.ScriptTarget.ES2020, {strict: true});
Dts.createSourceFile('file.ts', 'let y = 10;', ts.ScriptTarget.ES2020);
Attempts:
2 left
💡 Hint

Check the parameters of createSourceFile in the TypeScript compiler API.

🚀 Application
expert
3:00remaining
How many diagnostics are reported by this TypeScript program?

Given these two TypeScript files:

// file1.ts
const a: number = 'hello';

// file2.ts
const b: string = 123;

And this code to create a program and get diagnostics:

import ts from 'typescript';

const program = ts.createProgram(['file1.ts', 'file2.ts'], {});
const diagnostics = ts.getPreEmitDiagnostics(program);
console.log(diagnostics.length);

How many diagnostics will be printed?

A2
B0
C1
D4
Attempts:
2 left
💡 Hint

Each file has one type error. Think about how many errors the compiler reports.