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?
- let x: number = 5;
- let x = number(5);
- number x = 5;
- 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?
- A language of its own that only runs in the browser
- A superset of JavaScript that is compiled to JavaScript after type checking
- An animation library
- 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?
- They are checked on every access
- They are removed during compilation (type erasure)
- They are converted into objects
- 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";- n becomes a string
- Compiler error: string is not assignable to number
- Runtime error
- 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"?
- string[]
- array<string>
- [string]
- 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;?
- float
- number
- any
- 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?
- null
- void
- never
- 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?
- email?: string
- email: string?
- optional email: string
- 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"));- Output 3
- Output 12
- Compiler error
- 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?
- tsc app.ts
- ts app.ts
- node --compile app.ts
- 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?
- It turns off type checking for this value
- It only allows numbers
- It is a mandatory type for functions
- 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?
- id may be a string or a number
- id is a string and a number at the same time
- id is a tuple
- 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?
- An array of any length with strings and numbers
- A tuple: exactly two elements, first a string, then a number
- An object with two fields
- 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?
- Nothing
- With
unknownthe type must be checked before the value is used unknownonly allows numbersanyis 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));- 3
- 3.0
- Compiler error
- 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?
- It is invisible
- The compiler forbids assignments after initialization
- It is omitted from JSON
- 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?
- It removes all fields
- It makes all fields of
Nutzeroptional - It makes all fields read-only
- 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?
- A type with all fields except
name - A type that contains only the field
name - The value of
name - 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";- Nothing, strings are allowed
- Error: "gruen" is not assignable to type Farbe
- A runtime warning
- 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?
- A comparison
- A type parameter: a placeholder that is set or inferred at the call
- An HTML tag
- 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?
- For documentation
- The compiler can use it to narrow the variant unambiguously via if/switch (discriminated union)
- For runtime optimization
- 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?
- The function returns "string"
- Type predicate: on
truethe compiler knows thatxis a string - The function converts x to a string
- 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?
- It only creates a parameter
- It creates the private field
nameand assigns it automatically - It makes the class abstract
- It makes
namestatic
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?
- Only stricter formatting
- A group of strict checks, including strictNullChecks and noImplicitAny
- Faster compilation
- 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";- Nothing
- Error: el is possibly null
- Error: querySelector does not exist
- 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) { /* ... */ }- Error
- string
- unknown
- 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?
- Converts the value to a string at runtime
- Tells the compiler to treat the value as a string – without a runtime check
- Checks the type and throws on errors
- 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?
- Describe object shapes
- Merge multiple declarations (declaration merging)
- Use generics
- 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?
- number | string
- "a" | "b"
- ["a", "b"]
- 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;- { port: 8080; debug: false }
- { port: number; debug: boolean }
- object
- 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>;- "ja"
- "nein"
- string
- 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?
- It executes a promise
- It infers the inner type R of the promise
- It checks whether T is an object
- 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?
- Required<T>
- Readonly<T>
- Partial<T>
- 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?
- For optimization
- The compiler reports an error if a variant of the union was not handled
- It triggers an error at runtime
- 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?
- It only checks at runtime
- It checks assignability but keeps the more precise inferred type of the value
- It makes values read-only
- 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?
- For runtime validation
- To make structurally identical types (euro/dollar, different IDs) distinguishable
- To format numbers
- 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();- No, B is not A
- Yes, the types are structurally compatible
- Only with strict
- 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?
- Several implementations with the same name
- Several signatures, but exactly one implementation
- One signature, several implementations
- 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?
- Because
json()is slow - Because types do not exist at runtime –
aschecks nothing - Because
asonly works with classes - Because
Nutzeris 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?
- Nothing
- An enum generates real JavaScript code (an object), a union type disappears completely
- Enums are always faster
- 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>}`;- string
- "onKlick" | "onHover"
- "onklick" | "onhover"
- 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?
- It forbids arrays
- Index accesses like
liste[i]returnT | undefined - It checks indexes at runtime
- 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.