TSTypeScript

TypeScript quiz – 42 questions with answers

Try it interactively

With scoring, random questions, and saved progress – or go straight to the timed exam with certificate.

Beginner (13 questions)

How do you give a variable x the type number?

  1. let x: number = 5;
  2. let x = number(5);
  3. number x = 5;
  4. let x as number = 5;
Show answer

Correct: let x: number = 5;

The type comes after a colon following the name: let x: number = 5;.

What is TypeScript?

  1. A language of its own that only runs in the browser
  2. A superset of JavaScript that is compiled to JavaScript after type checking
  3. An animation library
  4. A replacement for Node.js
Show answer

Correct: A superset of JavaScript that is compiled to JavaScript after type checking

TypeScript extends JavaScript with types; the result is normal JavaScript that runs anywhere.

What happens to type annotations at runtime?

  1. They are checked on every access
  2. They are removed during compilation (type erasure)
  3. They are converted into objects
  4. They make the code faster
Show answer

Correct: They are removed during compilation (type erasure)

Types exist only at compile time. They are no longer present in the generated JavaScript.

What happens with this code?

let n = 5;
n = "five";
  1. n becomes a string
  2. Compiler error: string is not assignable to number
  3. Runtime error
  4. n becomes NaN
Show answer

Correct: Compiler error: string is not assignable to number

TypeScript infers the type number from 5. Assigning a string violates this type.

How do you write the type "array of strings"?

  1. string[]
  2. array<string>
  3. [string]
  4. string{}
Show answer

Correct: string[]

string[] (or, equivalently, Array<string>). [string] would be a tuple with exactly one element.

Which type does TypeScript infer for let x = 3.14;?

  1. float
  2. number
  3. any
  4. decimal
Show answer

Correct: number

JavaScript and TypeScript only have number for all numbers – whole or fractional.

Which type describes a function that returns nothing?

  1. null
  2. void
  3. never
  4. empty
Show answer

Correct: void

void stands for "no usable return value". never, by contrast, means the function does not return at all.

How do you mark a field of an interface as optional?

  1. email?: string
  2. email: string?
  3. optional email: string
  4. email: string | optional
Show answer

Correct: email?: string

The question mark after the field name makes it optional (the type is then string | undefined).

What happens with this code?

function f(a: number, b: number): number {
  return a + b;
}
console.log(f(1, "2"));
  1. Output 3
  2. Output 12
  3. Compiler error
  4. Output NaN
Show answer

Correct: Compiler error

The compiler reports: argument of type string is not assignable to a parameter of type number. Plain JavaScript would output "12".

Which command compiles app.ts to JavaScript?

  1. tsc app.ts
  2. ts app.ts
  3. node --compile app.ts
  4. npm build app.ts
Show answer

Correct: tsc app.ts

tsc is the TypeScript compiler; npx tsc app.ts produces app.js.

What does the type any do?

  1. It turns off type checking for this value
  2. It only allows numbers
  3. It is a mandatory type for functions
  4. It produces an error
Show answer

Correct: It turns off type checking for this value

any allows every operation without checking – errors only show up at runtime. unknown is better.

What does let id: string | number; mean?

  1. id may be a string or a number
  2. id is a string and a number at the same time
  3. id is a tuple
  4. id is optional
Show answer

Correct: id may be a string or a number

The vertical bar forms a union type: the value is one of the listed types.

What does [string, number] describe?

  1. An array of any length with strings and numbers
  2. A tuple: exactly two elements, first a string, then a number
  3. An object with two fields
  4. A union type
Show answer

Correct: A tuple: exactly two elements, first a string, then a number

A tuple fixes the length and the type of each position, e.g. ["Ada", 36].

Intermediate (15 questions)

What is the difference between unknown and any?

  1. Nothing
  2. With unknown the type must be checked before the value is used
  3. unknown only allows numbers
  4. any is safer
Show answer

Correct: With unknown the type must be checked before the value is used

unknown is the safe counterpart to any: it enforces a type check (narrowing) before use.

What does this code print?

function f(x: string | number) {
  if (typeof x === "string") {
    return x.toUpperCase();
  }
  return x.toFixed(1);
}
console.log(f(3));
  1. 3
  2. 3.0
  3. Compiler error
  4. NaN
Show answer

Correct: 3.0

In the string branch x is narrowed; otherwise x is a number and toFixed(1) returns "3.0".

What does readonly do on a property?

  1. It is invisible
  2. The compiler forbids assignments after initialization
  3. It is omitted from JSON
  4. It is only allowed in classes
Show answer

Correct: The compiler forbids assignments after initialization

readonly is a compiler check: after creation the property may no longer be assigned.

What does Partial<Nutzer> do?

  1. It removes all fields
  2. It makes all fields of Nutzer optional
  3. It makes all fields read-only
  4. It copies the type
Show answer

Correct: It makes all fields of Nutzer optional

Partial<T> creates a type in which every property of T is optional – ideal for update data.

What does Pick<Nutzer, "name"> produce?

  1. A type with all fields except name
  2. A type that contains only the field name
  3. The value of name
  4. An error
Show answer

Correct: A type that contains only the field name

Pick selects fields, Omit removes them.

What does the compiler report here?

type Farbe = "rot" | "blau";
const f: Farbe = "gruen";
  1. Nothing, strings are allowed
  2. Error: "gruen" is not assignable to type Farbe
  3. A runtime warning
  4. Error only with strict
Show answer

Correct: Error: "gruen" is not assignable to type Farbe

A literal union allows only the listed values.

What does <T> mean in function id<T>(x: T): T?

  1. A comparison
  2. A type parameter: a placeholder that is set or inferred at the call
  3. An HTML tag
  4. A class name
Show answer

Correct: A type parameter: a placeholder that is set or inferred at the call

Generics make functions reusable for many types without losing the type.

What is a common literal field like art: "kreis" in a union used for?

  1. For documentation
  2. The compiler can use it to narrow the variant unambiguously via if/switch (discriminated union)
  3. For runtime optimization
  4. It is mandatory for unions
Show answer

Correct: The compiler can use it to narrow the variant unambiguously via if/switch (discriminated union)

Through the discriminator field TypeScript knows in every branch which other fields are present.

What does the return type x is string mean?

  1. The function returns "string"
  2. Type predicate: on true the compiler knows that x is a string
  3. The function converts x to a string
  4. x is marked as a constant
Show answer

Correct: Type predicate: on true the compiler knows that x is a string

Custom type guards like this narrow types in the calling function.

What does constructor(private name: string) {} do?

  1. It only creates a parameter
  2. It creates the private field name and assigns it automatically
  3. It makes the class abstract
  4. It makes name static
Show answer

Correct: It creates the private field name and assigns it automatically

A modifier in front of a constructor parameter (parameter property) saves the field declaration and the assignment.

What does "strict": true enable in tsconfig.json?

  1. Only stricter formatting
  2. A group of strict checks, including strictNullChecks and noImplicitAny
  3. Faster compilation
  4. A ban on classes
Show answer

Correct: A group of strict checks, including strictNullChecks and noImplicitAny

strict bundles the most important checks. New projects should always start with it.

What does the compiler report with strictNullChecks?

const el = document.querySelector("#a");
el.textContent = "x";
  1. Nothing
  2. Error: el is possibly null
  3. Error: querySelector does not exist
  4. Only a runtime warning
Show answer

Correct: Error: el is possibly null

querySelector returns Element | null. You have to check for null (if (el)) or use el?..

What type does e have in the catch block with strict?

try { /* ... */ } catch (e) { /* ... */ }
  1. Error
  2. string
  3. unknown
  4. any
Show answer

Correct: unknown

With useUnknownInCatchVariables (part of strict) e has the type unknown – so check instanceof Error.

What does wert as string do?

  1. Converts the value to a string at runtime
  2. Tells the compiler to treat the value as a string – without a runtime check
  3. Checks the type and throws on errors
  4. Creates a new type
Show answer

Correct: Tells the compiler to treat the value as a string – without a runtime check

as is a pure compiler instruction. If the type is wrong, you only notice at runtime.

What can an interface do that a type alias cannot?

  1. Describe object shapes
  2. Merge multiple declarations (declaration merging)
  3. Use generics
  4. Extend other types
Show answer

Correct: Merge multiple declarations (declaration merging)

Interfaces with the same name are merged – this is how you extend global types such as Window. Unions and tuples, on the other hand, are only possible with type.

Advanced (14 questions)

What does keyof { a: number; b: string } produce?

  1. number | string
  2. "a" | "b"
  3. ["a", "b"]
  4. string
Show answer

Correct: "a" | "b"

keyof returns the key names as a union of literal types.

What type does Konf have?

const konf = { port: 8080, debug: false };
type Konf = typeof konf;
  1. { port: 8080; debug: false }
  2. { port: number; debug: boolean }
  3. object
  4. any
Show answer

Correct: { port: number; debug: boolean }

typeof in type position picks up the inferred type of the value – the fields are widened to number and boolean.

What does the type A evaluate to?

type IstString<T> = T extends string ? "ja" : "nein";
type A = IstString<number>;
  1. "ja"
  2. "nein"
  3. string
  4. never
Show answer

Correct: "nein"

number does not extend string, so the branch after the colon applies.

What does infer do in T extends Promise<infer R> ? R : T?

  1. It executes a promise
  2. It infers the inner type R of the promise
  3. It checks whether T is an object
  4. It creates a runtime variable
Show answer

Correct: It infers the inner type R of the promise

infer R gives the matching part of the type a name so that you can use it in the result.

Which built-in type is { [K in keyof T]?: T[K] } equivalent to?

  1. Required<T>
  2. Readonly<T>
  3. Partial<T>
  4. Pick<T, K>
Show answer

Correct: Partial<T>

The question mark in the mapped type makes all properties optional – exactly what Partial does.

What is const x: never = wert; in the default branch of a switch used for?

  1. For optimization
  2. The compiler reports an error if a variant of the union was not handled
  3. It triggers an error at runtime
  4. It ends the program
Show answer

Correct: The compiler reports an error if a variant of the union was not handled

If all variants are handled, wert has the type never in the default. If one is missing, the type does not fit – that is the exhaustiveness check.

What does satisfies offer compared to a type annotation?

  1. It only checks at runtime
  2. It checks assignability but keeps the more precise inferred type of the value
  3. It makes values read-only
  4. It is only for classes
Show answer

Correct: It checks assignability but keeps the more precise inferred type of the value

With satisfies you get errors for typos but still have precise types (e.g. a tuple instead of a wide union).

What are branded types (e.g. number & { __marke: "Euro" }) used for?

  1. For runtime validation
  2. To make structurally identical types (euro/dollar, different IDs) distinguishable
  3. To format numbers
  4. To replace classes
Show answer

Correct: To make structurally identical types (euro/dollar, different IDs) distinguishable

The brand exists only in the type system and prevents mix-ups without runtime cost.

Does this code compile?

class A { x = 1; }
class B { x = 2; }
const a: A = new B();
  1. No, B is not A
  2. Yes, the types are structurally compatible
  3. Only with strict
  4. No, because of duplicate names
Show answer

Correct: Yes, the types are structurally compatible

TypeScript uses structural typing: what counts is whether the shape fits, not the name of the class.

How is an overloaded function structured?

  1. Several implementations with the same name
  2. Several signatures, but exactly one implementation
  3. One signature, several implementations
  4. It is not possible in TypeScript
Show answer

Correct: Several signatures, but exactly one implementation

The overload signatures describe the ways to call it; the single implementation must cover all of them.

Why is (await res.json()) as Nutzer not enough to validate API data?

  1. Because json() is slow
  2. Because types do not exist at runtime – as checks nothing
  3. Because as only works with classes
  4. Because Nutzer is an interface
Show answer

Correct: Because types do not exist at runtime – as checks nothing

The compiler trusts the assertion. For real safety you need a runtime check (type guard or Zod).

What is the difference between an enum and a union type of literals?

  1. Nothing
  2. An enum generates real JavaScript code (an object), a union type disappears completely
  3. Enums are always faster
  4. Unions cannot contain strings
Show answer

Correct: An enum generates real JavaScript code (an object), a union type disappears completely

That is why many projects prefer unions or as const objects – also because they work with type stripping (Node.js).

Which type results here?

type Ereignis = "klick" | "hover";
type Handler = `on${Capitalize<Ereignis>}`;
  1. string
  2. "onKlick" | "onHover"
  3. "onklick" | "onhover"
  4. never
Show answer

Correct: "onKlick" | "onHover"

Template literal types generate a string literal type for each union member; Capitalize makes the first letter uppercase.

What does the compiler option noUncheckedIndexedAccess change?

  1. It forbids arrays
  2. Index accesses like liste[i] return T | undefined
  3. It checks indexes at runtime
  4. It makes arrays read-only
Show answer

Correct: Index accesses like liste[i] return T | undefined

This forces you to handle possibly missing elements instead of wrongly assuming that every index exists.