TypeScript

TypeScript


Beginner

Q1: What is TypeScript?

TypeScript is a statically typed superset of JavaScript that adds type checking and other developer tooling improvements.

Q2: Why use TypeScript instead of JavaScript?

TypeScript helps catch bugs earlier, improves tooling, and makes large codebases easier to maintain.

Q3: What is a superset?

A superset is a language that extends another language without removing its features.

Q4: Is TypeScript a runtime language?

TypeScript is compiled to JavaScript before execution, so the runtime is still JavaScript.

Q5: What is the TypeScript compiler?

The TypeScript compiler, usually invoked as `tsc`, converts TypeScript code into JavaScript.

Q6: What is `tsconfig.json`?

`tsconfig.json` configures the TypeScript compiler and project settings.

Q7: What does `strict` mode do?

`strict` enables stricter checks like `noImplicitAny`, `strictNullChecks`, and others.

Q8: What is `noImplicitAny`?

It disallows implicit `any` types, which helps catch mistakes earlier.

Q9: What is `strictNullChecks`?

It prevents `null` and `undefined` from being assigned to types unless explicitly allowed.

Q10: What is JavaScript?

JavaScript is the language executed by browsers and Node.js with dynamic typing.

Q11: What are types?

Types describe the kind of value a variable or function can hold.

Q12: Why are types useful?

They improve readability, catch bugs, and enable editor support.

Q13: What is a variable?

A variable stores a value that can be used later in code.

Q14: What is `let` in TypeScript?

`let` declares a block-scoped variable.

Q15: What is `const` in TypeScript?

`const` declares a block-scoped constant that cannot be reassigned.

Q16: What is the difference between `let` and `const`?

`let` can be reassigned; `const` cannot be reassigned.

Q17: What is a primitive type?

A primitive type is a basic value type, such as `string`, `number`, `boolean`, `null`, `undefined`, or `symbol`.

Q18: What is `string`?

`string` represents textual data.

Q19: What is `number`?

`number` represents numeric values, including integers and decimals.

Q20: What is `boolean`?

`boolean` represents `true` or `false`.

Q21: What is `null`?

`null` represents an intentional absence of a value.

Q22: What is `undefined`?

`undefined` means a value has not been assigned yet.

Q23: What is `any`?

`any` disables type checking for a value and should be used carefully.

Q24: Why avoid `any`?

Because it removes the safety TypeScript provides.

Q25: What is a function?

A function is a reusable block of logic.

Q26: How do you declare a function in TypeScript?

Example: `function greet(name: string): string { return name; }`

Q27: What is a parameter?

A parameter is a value passed into a function.

Q28: What is a return type?

A return type describes the value a function returns.

Q29: What is a function signature?

A function signature includes the parameter types and return type.

Q30: What is an object type?

An object type defines the shape of an object, including properties and their types.

Q31: What is an interface?

An interface declares the structure of an object, function, or class contract.

Q32: What is a type alias?

A type alias gives a name to a type, such as a primitive, object, or union.

Q33: What is an array?

An array is a collection of values.

Q34: What is `Array<number>`?

It is an array of numbers.

Q35: What is `number[]`?

It is a shorthand for an array of numbers.

Q36: What is a tuple?

A tuple is an array with a fixed number of positions and potentially different types.

Q37: What is `enum`?

An enum defines a set of named constants.

Q38: What is a union type?

A union allows a value to be one of several types.

Q39: What is an intersection type?

An intersection combines multiple types into one.

Q40: What is `unknown`?

`unknown` is a type that represents values without knowing their type, requiring a type check before use.

Q41: What is `never`?

`never` represents values that never occur, such as a function that always throws.

Q42: What is `void`?

`void` means a function returns no meaningful value.

Q43: What is `nullish coalescing`?

`??` returns the right side only when the left side is `null` or `undefined`.

Q44: What is optional chaining?

`?.` safely accesses nested property values without crashing on `null` or `undefined`.

Q45: What is `as` casting?

`as` tells TypeScript to treat a value as a specific type.

Q46: What is a type assertion?

A type assertion is a compile-time declaration that a value has a given type.

Q47: What is `const` assertion?

A const assertion preserves literal types and prevents widening.

Q48: What is `readonly`?

`readonly` prevents reassignment of a property after initialization.

Q49: What is a class?

A class is a template for creating objects with data and methods.

Q50: What is a method?

A method is a function defined on a class.

Q51: What is a constructor?

A constructor initializes a new instance of a class.

Q52: What is a property?

A property is a value stored on an object or class instance.

Q53: What is `public`?

`public` means the property or method is accessible from outside the class.

Q54: What is `private`?

`private` restricts access to the class itself.

Q55: What is `protected`?

`protected` allows access only inside the class and subclasses.

Q56: What is inheritance?

Inheritance allows a class to extend another class and reuse its behavior.

Q57: What is a generic?

A generic allows types to be parameterized and reused across multiple values.

Q58: What is `Array<T>`?

It represents an array of values of type `T`.

Q59: What is a function generic?

A generic function accepts type parameters for flexibility.

Q60: What is a module?

A module is a TypeScript file that can export and import code.

Q61: What is `export`?

`export` makes a value available to other modules.

Q62: What is `import`?

`import` brings values from another module into the current file.

Q63: What is a default export?

A default export is the main exported value from a module.

Q64: What is a named export?

A named export exposes a particular symbol from a module.

Q65: What is a namespace?

A namespace groups values and types under a common name.

Q66: What is `ts-node`?

`ts-node` runs TypeScript files directly without a build step.

Q67: What is `tsc`?

`tsc` is the TypeScript compiler.

Q68: What is a transpile?

Transpile means converting TypeScript to JavaScript.

Q69: What is `target` in TypeScript?

It sets the JavaScript version the compiler emits.

Q70: What is `module` in TypeScript?

It sets the module system emitted, such as CommonJS or ESNext.

Q71: What is a literal type?

A literal type is a specific value such as `"admin"` or `42`.

Q72: What is a template literal type?

It allows a type to be built from string patterns.

Q73: What is `keyof`?

`keyof` gets the keys of an object type.

Q74: What is `typeof` in a type context?

`typeof` gets the type of a value or expression.

Q75: What is `as const`?

It narrows values to literal types and prevents mutation.

Q76: What is a type guard?

A type guard narrows a union to a more specific type at runtime.

Q77: What is `in` operator type guard?

It checks whether a property exists on an object.

Q78: What is `instanceof`?

`instanceof` checks whether an object is an instance of a class.

Q79: What is `satisfies`?

`satisfies` checks that a value matches a type without widening it.

Q80: What is a declaration file?

A declaration file provides type definitions for JavaScript libraries.

Q81: What is `.d.ts`?

It is the extension for TypeScript declaration files.

Q82: What is `lib` in tsconfig?

`lib` configures which standard-library type definitions are included.

Q83: What is the DOM library?

It includes browser APIs and types such as `window` and `document`.

Q84: What is ESNext?

It enables the latest ECMAScript type definitions.

Q85: What is incremental compilation?

Incremental compilation stores build information to speed up future compiles.

Q86: Why compile TypeScript?

Because browsers and Node.js do not natively run TypeScript.

Q87: What is a type declaration package?

A package like `@types/node` or `@types/react` provides TypeScript types for a JS library.

Q88: What is `npm`?

`npm` is the package manager used to install JavaScript and TypeScript dependencies.

Q89: What is a type-only import?

An import used only for types, often written as `import type`.

Q90: What is `never` used for?

It is used to represent impossible states, often in exhaustive checks.

Q91: What is an index signature?

An index signature allows an object to have dynamic keys with a given type.

Q92: What is `Record<K, T>`?

`Record<K, T>` creates an object type with keys of type `K` and values of type `T`.

Q93: What is `Partial<T>`?

`Partial<T>` makes all properties optional.

Q94: What is `Required<T>`?

`Required<T>` makes all properties required.

Q95: What is `Pick<T, K>`?

`Pick<T, K>` creates a type using only selected keys.

Q96: What is `Omit<T, K>`?

`Omit<T, K>` creates a type without selected keys.

Q97: What is `ReadonlyArray<T>`?

It is an array type whose contents cannot be mutated.

Q98: What is a destructuring assignment?

It extracts values from arrays or objects into variables.

Q99: What is object destructuring?

Example: `const { name } = user;`

Q100: What is array destructuring?

Example: `const [first, second] = items;`

Intermediate

Q101: What is type inference?

Type inference lets TypeScript infer types automatically without explicit annotation.

Q102: Why is inference useful?

It reduces boilerplate while keeping type safety.

Q103: What is contextual typing?

Contextual typing infers types from the surrounding code, such as function arguments or object literals.

Q104: What is a function overload?

Function overloads let one function accept multiple signatures.

Q105: Why use overloads?

To model APIs that accept different parameter combinations.

Q106: What is a rest parameter?

A rest parameter collects multiple arguments into an array.

Q107: What is a spread operator?

A spread operator expands arrays or object values into other arrays or objects.

Q108: What is a tuple with labels?

A labeled tuple element gives names to tuple positions.

Q109: What is `this` typing in TypeScript?

TypeScript can model `this` within methods and functions.

Q110: What is a class field initializer?

A field initializer assigns an initial value to a class property.

Q111: What is a getter?

A getter computes a property value dynamically.

Q112: What is a setter?

A setter assigns a value when a property is set.

Q113: What is abstract class?

An abstract class cannot be instantiated directly and may define abstract methods.

Q114: What is an abstract method?

An abstract method must be implemented by subclasses.

Q115: What is an interface extending another interface?

It inherits the properties and methods of the base interface.

Q116: What is a class implementing an interface?

It promises to satisfy the interface contract.

Q117: What is method overriding?

A subclass provides a new implementation of a parent method.

Q118: What is polymorphism?

Polymorphism allows different objects to be handled through a shared interface or base class.

Q119: What is a discriminated union?

A discriminated union uses a common property to distinguish variants.

Q120: What is `never` in exhaustive switch checks?

It is used to assert that all union variants have been handled.

Q121: What is a mapped type?

A mapped type transforms all properties of an existing type.

Q122: What is `keyof` with mapped types?

It lets you derive a new object type from the keys of an existing type.

Q123: What is `TemplateStringsArray`?

It is the type for template literal strings with dynamic substitutions.

Q124: What is a conditional type?

A conditional type creates a type based on a condition.

Q125: What is `infer`?

`infer` extracts a type from another type within a conditional type.

Q126: What is variadic tuple type?

It is a tuple type that can accept a variable number of elements.

Q127: What is a generic constraint?

A generic constraint limits which types can be used for a generic parameter.

Q128: What is `extends` in a generic constraint?

Example: `T extends Record<string, unknown>`.

Q129: What is `default` type parameter?

It is a fallback type used if no type is explicitly provided.

Q130: What is a declaration merging?

Declaration merging combines multiple declarations of the same symbol.

Q131: What is a namespace merging with interfaces?

It allows multiple interface declarations to be combined.

Q132: What is a module augmentation?

It adds extra declarations to an existing module.

Q133: What is `import type`?

It imports only types and excludes runtime JS output.

Q134: What is `export type`?

It exports a type only, without any runtime value.

Q135: What is the difference between `type` and `interface`?

Interfaces are open and mergeable; type aliases are more flexible but not mergeable.

Q136: What is a structural type system?

TypeScript checks compatibility by structure rather than by inheritance hierarchy alone.

Q137: Why is structural typing useful?

It allows compatible object shapes to be used interchangeably.

Q138: What is a duck typing?

Duck typing is the idea that if an object has the needed properties and methods, it is considered compatible.

Q139: What is a function type?

A function type describes a callable value.

Q140: What is a callback type?

A callback type is a function passed as a parameter to another function.

Q141: What is an async function?

An async function returns a Promise.

Q142: What is `Promise<T>`?

It represents a future value of type `T`.

Q143: What is `await`?

`await` waits for a promise to resolve before continuing.

Q144: What is `try/catch` in TypeScript?

It handles runtime errors in synchronous and async code.

Q145: What is a rejected promise?

A Promise that fails and causes an error path in async code.

Q146: What is `unknown` vs `any`?

`unknown` is safer because it requires checking before use.

Q147: What is a `Record<string, unknown>` type?

It represents an object with string keys and unknown values.

Q148: What is a `readonly` array?

A readonly array prevents mutation.

Q149: What is literal narrowing?

TypeScript narrows a union based on literal values.

Q150: What is exhaustiveness checking?

It ensures a switch statement handles every possible variant.

Q151: What is an enum reverse mapping?

In JavaScript, numeric enums create reverse mappings, which can be surprising.

Q152: What is string enum?

A string enum holds string values.

Q153: Why prefer string literal unions over plain enums sometimes?

They are often simpler and easier to reason about in modern TypeScript.

Q154: What is a type predicate?

A type predicate is an assertion used by type guards to narrow types.

Q155: What is a custom type guard?

A function that returns `value is SomeType`.

Q156: What is `asserts` return type?

It is a type predicate form that asserts a condition rather than returning a narrowed value.

Q157: What is `unknown` in a DOM API?

It often appears when parsing unknown data from external sources.

Q158: What is a `null`-safe pattern?

It uses optional chaining and checks to prevent `null` or `undefined` errors.

Q159: What is a type widening?

Type widening broadens a literal into a wider primitive type.

Q160: What is a type narrowing?

Type narrowing refines a broader type into a more specific one.

Q161: What is an index access type?

Example: `T["prop"]` selects the type of a property.

Q162: What is `keyof` on a union type?

It produces the keys common to the union.

Q163: What is `ReturnType<T>`?

It gets the return type of a function type.

Q164: What is `Parameters<T>`?

It gets the parameter tuple of a function type.

Q165: What is `ConstructorParameters<T>`?

It gets the constructor parameter tuple of a class type.

Q166: What is `InstanceType<T>`?

It gets the instance type of a constructor function or class.

Q167: What is interface extension with class types?

An interface can extend a class type and inherit members from it.

Q168: What is a `private` property in structural typing?

Compatibility is impacted by private fields, because TypeScript checks that private members match exactly.

Q169: What is a `protected` property in classes?

It is accessible within the class and derived classes.

Q170: What is method overload resolution?

TypeScript chooses the most appropriate overload signature for a call.

Q171: What is a generic default type constraint?

A default generic lets a type parameter have a default if not specified.

Q172: What is `this` parameter in functions?

It models the calling context explicitly in function signatures.

Q173: What is a function returning `this`?

It can be used to model fluent APIs.

Q174: What is an `as` assertion for DOM APIs?

It is common when elements are typed more generally than their actual interface.

Q175: What is `satisfies` useful for?

It ensures object literal shapes match a type without losing specific literal types.

Q176: What is `const` assertion in object literals?

It preserves literal values and prevents accidental widening.

Q177: What is declaration file generation?

It is when TypeScript generates `.d.ts` files for compile output.

Q178: What is `–declaration` in tsconfig?

It generates declaration files alongside output JavaScript.

Q179: What is the effect of `–emitDeclarationOnly`?

It emits only type declaration files, not JavaScript.

Q180: What is the difference between compile-time and runtime behavior?

TypeScript types are erased at runtime; they only exist for tooling and static checks.

Advanced / Expert

Q181: What is a higher-order function?

A function that takes or returns functions.

Q182: What is a function composition pattern?

It combines small functions into a larger pipeline.

Q183: What is currying?

Currying transforms a function with multiple arguments into a sequence of functions taking one argument at a time.

Q184: What is a monad in TypeScript?

It is a design pattern for wrapping values and chaining operations, often represented by `Promise`, `Maybe`, or custom types.

Q185: What is a `Result` type?

A `Result` type often encodes success or failure without throwing exceptions.

Q186: What is `Either`?

`Either` is a type representing two possible outcomes, often used for error handling.

Q187: What is a `Maybe` or `Option` type?

It models optional values without `null`/`undefined` checks everywhere.

Q188: What is type-level programming?

It is using types themselves to express computations and constraints.

Q189: What is a recursive type?

A type that refers to itself, such as a tree or linked list.

Q190: What is a `type` that references itself?

Example: `type Tree = { value: string; children: Tree[] }`

Q191: What are mapped types with `as` remapping?

It allows renaming keys or creating derived object shapes.

Q192: What is a template literal mapping?

It allows building object keys or types from string patterns.

Q193: What is a branded type?

A branded type uses a unique property to distinguish semantically different values with the same runtime representation.

Q194: What is a nominal type system?

A nominal system distinguishes types by name, not just shape.

Q195: How is TypeScript different from nominal typing?

TypeScript is mostly structural, though brands can emulate nominal behavior.

Q196: What is a type-level function?

A type-level function computes a new type based on input types.

Q197: What is `infer` in advanced type patterns?

It extracts a type from a conditional type and helps create complex type utilities.

Q198: What is a distributive conditional type?

It distributes over unions, which can change the resulting type behavior.

Q199: What is `declare global`?

It augments the global namespace in TypeScript.

Q200: What is the main lesson of TypeScript?

TypeScript is not just about types; it is about creating safer, more expressive, and more maintainable code while still running on JavaScript runtimes.