Beginner TypeScript interview questions
Q1 Beginner Classes: Access Modifiers, Abstract Classes & Implements
Short answer
public (the default) makes a member accessible from anywhere; private restricts access to only within the declaring class itself; protected allows access within the declaring class AND its subclasses, but not from outside code.
Full answer with examples Q2 Beginner Conditional Types
Short answer
A conditional type has the form `T extends U ? X : Y` — evaluated at the type level, it resolves to X if T is assignable to U, and Y otherwise, mirroring the syntax and logic of a runtime ternary expression but operating purely on types.
Full answer with examples Q3 Beginner Declaration Files
Short answer
A declaration file contains only type information — interfaces, type aliases, and function/variable signatures — with no runtime implementation code.
Full answer with examples Q4 Beginner Decorators
Short answer
A decorator is a special kind of declaration prefixed with @ that can be attached to a class, method, property, or parameter to observe, modify, or replace its definition — it's essentially a function that runs at class-definition time, wrapping or annotating the target.
Full answer with examples Q5 Beginner Discriminated Unions & Exhaustiveness Checking
Short answer
A discriminated union is a union of object types that all share a common literal-typed property (the discriminant, often called 'kind' or 'type').
Full answer with examples Q6 Beginner Enums
Short answer
A numeric enum assigns sequential integer values starting at 0 to each member by default, unless you explicitly assign a starting value — subsequent members then auto-increment from there.
Full answer with examples Q7 Beginner Function Types, Overloads & Optional/Default Parameters
Short answer
Use arrow-style syntax `(param: Type) => ReturnType` to describe a function's shape, specifying each parameter's type and the return type.
Full answer with examples Q8 Beginner Generics
Short answer
'any' disables type checking entirely, losing all safety and autocomplete. Generics let a function or type work with MULTIPLE specific types while preserving the actual relationship between input and output types — the compiler still knows exactly what type comes out based on…
Full answer with examples Q9 Beginner Index Signatures & Record Types
Short answer
An index signature describes the type of ALL properties an object can have when the exact set of key names isn't known ahead of time, using the syntax `{ [key: string]: ValueType }` — it lets you type dictionary-like objects with dynamic keys.
Full answer with examples Q10 Beginner Mapped Types
Short answer
A mapped type generates a new object type by iterating over the keys of an existing type T (via `keyof T`) and applying a transformation to each property's type, letting you derive new types from existing ones without manually re-listing every property.
Full answer with examples Q11 Beginner Migrating JavaScript to TypeScript
Short answer
Add a tsconfig.json with 'allowJs': true and 'checkJs': false (or start with a very lenient config), rename files gradually from .js to .ts rather than all at once, and let the project compile alongside existing JavaScript during the transition.
Full answer with examples Q12 Beginner Modules
Short answer
TypeScript's import/export statements are (mostly) standard ES Module syntax with type-only extensions layered on top; the 'module' compiler option in tsconfig.json controls what actual JavaScript module format (CommonJS, ESNext, ES2020, etc.) this syntax gets compiled down to…
Full answer with examples Q13 Beginner Namespaces & Declaration Merging
Short answer
A namespace groups related code under a single named container using the 'namespace' keyword, generating a nested object at runtime — it was TypeScript's original way to organize code before ES Modules became the standard, and is mostly used today for organizing types or in…
Full answer with examples Q14 Beginner Never, Unknown & Void Types
Short answer
'void' indicates a function's return value should be IGNORED by callers, even if the function actually returns undefined (or nothing at all) at runtime — it's specifically about the CALLER's perspective and intent, distinct from 'undefined' which is about the actual returned…
Full answer with examples Q15 Beginner Nullability: strictNullChecks, Optional Chaining & Nullish Coalescing
Short answer
Without strictNullChecks, null and undefined are silently assignable to EVERY type (a major source of runtime errors). With it enabled, null and undefined become distinct types that must be explicitly included in a type's union (e.g.
Full answer with examples Q16 Beginner Primitive Types, Arrays & Tuples
Short answer
TypeScript's core primitives are string, number, boolean, null, undefined, symbol, and bigint — annotate a variable using a colon followed by the type name after its identifier.
Full answer with examples Q17 Beginner Readonly, const Assertions & Immutability
Short answer
'const' prevents REASSIGNING a variable binding itself, but doesn't stop you from mutating an object it points to; 'readonly' on a property prevents REASSIGNING that specific property after the object is created, while the object binding itself might still be a regular mutable…
Full answer with examples Q18 Beginner Structural Typing & Duck Typing
Short answer
Structural typing determines type compatibility based purely on the SHAPE of a type — what properties/methods it has — rather than its declared NAME or explicit inheritance relationship (nominal typing).
Full answer with examples Q19 Beginner Template Literal Types
Short answer
A template literal type uses the same backtick syntax as a runtime template literal, but with TYPES embedded inside `${...}` instead of values — it produces a new string literal type (or union of them) built from combining literal text with the possible values of the embedded…
Full answer with examples Q20 Beginner tsconfig & Compiler Options
Short answer
tsconfig.json marks a directory as the root of a TypeScript project and configures how the compiler behaves — 'compilerOptions' specifically holds settings controlling type-checking strictness, the JavaScript output target/format, module resolution, and where compiled files are…
Full answer with examples Q21 Beginner Classes: Access Modifiers, Abstract Classes & Implements
Short answer
The 'private' keyword is a TypeScript-only, compile-time restriction — it's erased at compile time and the field is still a regular accessible property in the emitted JavaScript.
Full answer with examples Q22 Beginner Conditional Types
Short answer
Combine a conditional type with the 'infer' keyword to capture and name the array's element type only when T actually matches the array shape.
Full answer with examples Q23 Beginner Declaration Files
Short answer
Install the corresponding community-maintained package from DefinitelyTyped, prefixed with @types/ — TypeScript automatically discovers and uses these type packages from node_modules without any extra configuration.
Full answer with examples Q24 Beginner Decorators
Short answer
Set "experimentalDecorators": true in compilerOptions — without it, the @ decorator syntax produces a compile error under the legacy decorator implementation used by frameworks like older versions of Angular and NestJS.
Full answer with examples Q25 Beginner Discriminated Unions & Exhaustiveness Checking
Short answer
Switch on the discriminant property; inside each case block, TypeScript automatically narrows the variable to the specific variant matching that case's literal value, giving you safe access to that variant's unique properties.
Full answer with examples Q26 Beginner Enums
Short answer
A string enum explicitly assigns a distinct string literal to each member (no auto-incrementing), which makes debugging easier since logged/serialized values are meaningful text rather than opaque numbers, and prevents accidental numeric type coercion between unrelated enums.
Full answer with examples Q27 Beginner Function Types, Overloads & Optional/Default Parameters
Short answer
Add a '?' after the parameter name; TypeScript automatically adds 'undefined' to its type, and optional parameters must come after all required parameters in the parameter list.
Full answer with examples Q28 Beginner Generics
Short answer
Add ` ` (or any name) right after the function name to declare a type parameter, then use T within the parameter and return types; you can let TypeScript infer T from the arguments, or explicitly specify it in angle brackets at the call site.
Full answer with examples Q29 Beginner Index Signatures & Record Types
Short answer
Record is a built-in utility type equivalent to `{ [key in K]: V }` — it's the more concise, commonly preferred way to express a dictionary-like object type where K is typically a string, number, or union of literal types and V is the value type.
Full answer with examples Q30 Beginner Mapped Types
Short answer
Partial is implemented as `{ [K in keyof T]?: T[K] }` — it maps over every key of T and adds a '?' to make each property optional, producing a version of T where every field can be omitted.
Full answer with examples Q31 Beginner Migrating JavaScript to TypeScript
Short answer
allowJs lets the TypeScript compiler process plain .js files alongside .ts files in the same project, so you can incrementally rename files to .ts one at a time while the rest of the codebase remains JavaScript and still compiles/type-checks together.
Full answer with examples Q32 Beginner Modules
Short answer
'import type' explicitly imports only type information, which the compiler completely erases from the output JavaScript — useful for making it clear an import has zero runtime cost, and required in some strict module setups (isolatedModules) to disambiguate type-only imports.
Full answer with examples Q33 Beginner Namespaces & Declaration Merging
Short answer
Just like a module, everything declared inside a namespace is private to that namespace by default; only explicitly 'export'ed members become accessible via NamespaceName.memberName from outside code.
Full answer with examples Q34 Beginner Never, Unknown & Void Types
Short answer
'unknown' represents a value whose type isn't known yet — like 'any', it can hold ANY value, but unlike 'any', you CANNOT perform any operations on an 'unknown' value (property access, calling it, arithmetic) until you first narrow it to a more specific type via a type guard…
Full answer with examples Q35 Beginner Nullability: strictNullChecks, Optional Chaining & Nullish Coalescing
Short answer
?. safely accesses a property, calls a method, or indexes into a value, short-circuiting and returning 'undefined' immediately if the value right before it is null or undefined, instead of throwing a TypeError.
Full answer with examples Q36 Beginner Primitive Types, Arrays & Tuples
Short answer
Use either `Type[]` (more common, especially for simple types) or the generic `Array ` syntax (sometimes preferred for more complex or nested generic types, for readability) — both are functionally identical.
Full answer with examples Q37 Beginner Readonly, const Assertions & Immutability
Short answer
Prefix the array type with 'readonly', or use the generic `ReadonlyArray ` — both prevent index assignment AND remove access to mutating methods like push, pop, splice, and sort from the type entirely.
Full answer with examples Q38 Beginner Structural Typing & Duck Typing
Short answer
Duck typing (the phrase's origin) means a value's suitability is judged by whether it HAS the behavior/shape you need, not by what it's formally declared to be — TypeScript's structural type system directly embodies this: any value with the right shape is accepted, regardless of…
Full answer with examples Q39 Beginner Template Literal Types
Short answer
When you embed a union type inside `${...}`, TypeScript computes the CROSS PRODUCT of every combination, producing a new union of every possible resulting string literal — this happens automatically, without any extra syntax needed.
Full answer with examples Q40 Beginner tsconfig & Compiler Options
Short answer
'target' specifies which ECMAScript version the compiled JavaScript output should conform to — newer language features (like optional chaining, async/await, or classes) get automatically transformed ('downleveled') into older equivalent syntax if the target doesn't natively…
Full answer with examples Q41 Beginner Classes: Access Modifiers, Abstract Classes & Implements
Short answer
Mark a class with the abstract keyword to prevent it from being instantiated directly with 'new' — it exists only to be extended, typically providing shared implementation plus one or more abstract methods that subclasses MUST implement.
Full answer with examples Q42 Beginner Function Types, Overloads & Optional/Default Parameters
Short answer
Assign a default value directly in the parameter list using '='; TypeScript infers the parameter's type from the default value's type unless you also add an explicit annotation, and the parameter becomes optional to callers.
Full answer with examples