TypeScript MCQ
Test your TypeScript knowledge with 100 multiple choice questions covering fundamentals to advanced concepts, with instant feedback and explanations.
What is TypeScript primarily known as?
2Which file extension is used for TypeScript files?
3Which command compiles a TypeScript file to JavaScript using the TypeScript compiler?
4How do you declare a variable named age of type number in TypeScript?
5Which type represents a value that can be any type and disables type checking?
6What does the "void" return type indicate for a function?
7How do you define an array of strings in TypeScript?
8What keyword is used to define a custom type alias?
9Which symbol marks a property as optional in an interface?
10What is the purpose of an interface in TypeScript?
11Which of these is a valid TypeScript union type declaration?
12What does the "readonly" modifier do to a property?
13Which TypeScript feature allows enumerating a fixed set of named constants?
14What is the default value of the first member in a numeric enum?
15How do you annotate a function parameter "name" as a string?
16Which configuration file is used to configure the TypeScript compiler options for a project?
17What does the "never" type represent?
18How do you specify that a function parameter is optional?
19Which keyword is used to implement an interface in a class?
20What is the correct syntax to create a tuple of a string and a number?
21What does the "as" keyword do in TypeScript?
22Which built-in utility type makes all properties of a type optional?
23What is the output type of "typeof" used in a type context, e.g. "type T = typeof someVar"?
24Which symbol is used for non-null assertion in TypeScript?
25How do you define a constant variable in TypeScript that cannot be reassigned?
26What does "strict mode" in tsconfig.json enable?
27Which of the following correctly defines a function type?
28What is "type inference" in TypeScript?
29Which access modifier makes a class member accessible only within the class itself?
30What is the default access modifier for class members in TypeScript if none is specified?
31How do you represent a value that could be either a string or null?
32What does "npx tsc --init" do?
33Which of these is a primitive type in TypeScript?
34How do you import a named export "add" from a module "math.ts"?
35What is the purpose of the "export" keyword?
36Which TypeScript feature lets you create a new type by extending an existing interface?
37What does the double equals (==) versus triple equals (===) distinction inherited from JavaScript mean in TypeScript?
38How do you define a class property with a default value in TypeScript?
39Which loop syntax iterates over the values of an array in TypeScript?
40What is the result type of "5 as const" assigned to a variable?
What is a generic function in TypeScript?
2What does the "keyof" operator produce?
3Given "interface Point { x: number; y: number; }", what is the type of "keyof Point"?
4What is the purpose of mapped types like "{ [K in keyof T]: boolean }"?
5What does the utility type "Pick<T, K>" do?
6What does the utility type "Omit<T, K>" do?
7What is a discriminated union in TypeScript?
8What does "strictNullChecks" do when enabled?
9What is the difference between an "interface" and a "type" alias for object shapes?
10What does the following do? "function isString(val: unknown): val is string { return typeof val === 'string'; }"
11What does "Record<K, V>" represent?
12What is the effect of declaring a class constructor parameter as "private readonly name: string"?
13What does the "satisfies" operator (introduced in TS 4.9) do?
14What is the purpose of "declare" in a .d.ts file?
15How does TypeScript handle excess property checks for object literals?
16What does "Partial<Required<T>>" effectively do for an interface T with optional and required properties?
17What is the result of widening for "let x = 'hello';" versus "const y = 'hello';"?
18What does the conditional type "T extends U ? X : Y" do?
19What is the purpose of the "infer" keyword in conditional types?
20What does "ReturnType<typeof myFunc>" give you?
21How do generic constraints work, e.g. "function logLength<T extends { length: number }>(arg: T)"?
22What is the difference between "unknown" and "any"?
23What does declaration merging allow for namespaces and interfaces with the same name?
24What is an abstract class used for in TypeScript?
25What does the "in" operator do as a type guard, e.g. "if ('swim' in animal)"?
26What is the purpose of generics default type parameters, e.g. "interface Box<T = string>"?
27How does TypeScript's structural typing differ from nominal typing?
28What does "Exclude<T, U>" do?
29What is "module augmentation" used for?
30What is the effect of "noImplicitAny" compiler option?
31What does the spread operator do when used with TypeScript tuples, e.g. "type T = [number, ...string[]]"?
32What is the purpose of function overloads in TypeScript?
33How does TypeScript treat enums with string values, e.g. "enum Color { Red = 'RED', Blue = 'BLUE' }"?
34What does "this" parameter typing, e.g. "function fn(this: HTMLElement, event: Event)", accomplish?
35What does the "Awaited<T>" utility type do?
36When using "import type { Foo } from './module'", what is the effect?
37What does "Array.isArray(value)" help with when narrowing a union type like "string | string[]"?
38What is the difference between "interface A extends B" and "type A = B & C"?
39What does the "as const" assertion do when applied to an array literal, e.g. "const arr = [1, 2, 3] as const"?
40What is the benefit of using "unknown" as the type for a caught error in a try/catch block, e.g. "catch (err: unknown)"?
What is the difference between covariance and contravariance in TypeScript function parameter typing?
2What does the following recursive conditional type compute? "type Flatten<T> = T extends Array<infer Item> ? Flatten<Item> : T"
3What is a "branded type" (or nominal typing emulation) pattern used for?
4Given "type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K] }", what does this mapped type achieve?
5What is the purpose of template literal types, e.g. "type Greeting = `Hello, ${string}!`"?
6What does "distributive conditional types" mean for "type ToArray<T> = T extends any ? T[] : never" when T is a union like "string | number"?
7What is the role of "asserts" in a function signature like "function assertIsString(val: unknown): asserts val is string"?
8How do variadic tuple types, e.g. "type Concat<T extends unknown[], U extends unknown[]> = [...T, ...U]", behave?
9What does the TypeScript compiler do differently with "const enum" compared to a regular "enum"?
10What does the "--isolatedModules" compiler flag warn against?
11How does TypeScript resolve overload resolution order when multiple overload signatures could match a call?
12What is the effect of "exactOptionalPropertyTypes" compiler option?
13What does the "this" type (polymorphic this) returned from a method enable in a fluent/chainable API?
14What is the purpose of "Function" overload combined with generic inference for a curried function, e.g. "function curry<A, B, C>(fn: (a: A, b: B) => C): (a: A) => (b: B) => C"?
15How does TypeScript handle circular type references, e.g. "type Json = string | number | boolean | null | Json[] | { [key: string]: Json }"?
16What does the "--strictPropertyInitialization" flag enforce for class properties?
17What is the difference between "Function" type and a specific function signature like "(a: number) => void" for type safety?
18What does the utility type combination "type Mutable<T> = { -readonly [K in keyof T]: T[K] }" do with the "-readonly" modifier?
19In TypeScript's control flow analysis, what does "Object.freeze(obj)" do to the inferred type of obj's properties when obj is a literal?
20What is "type narrowing via assignment" and how does control flow analysis use it, e.g. "let x: string | number = getValue(); x = 5;"?