TypeScript Answers


0:00
0:00

TypeScript Answers

Basic TypeScript Answers

#QuestionAnswerExamples
1What is TypeScript?A superset of JavaScript that adds static typingIt compiles down to plain JavaScript
2Why use TypeScript?Improves code maintainability, catches errors early, provides better toolingType checking during development, better code readability, autocompletion
3What is static typing?Checking variable types at compile time (before the code runs)let name: string = "Alice";, let age: number = 30;
4How do you declare a variable with a type?Using a colon (:) followed by the type namelet isActive: boolean = true;
5What are basic primitive types?string, number, boolean, null, undefined, symbol, bigintlet message: string = "Hello";, let count: number = 100;
6How do you define an array type?Using Type[] or Arraylet names: string[] = ["Bob", "Charlie"];, let numbers: Array = [1, 2, 3];
7How do you define an object type?Using an interface or type aliasinterface User { name: string; age: number; } let user: User = { name: "Dave", age: 40 };
8What is an interface?Defines the shape of an objectinterface Point { x: number; y: number; }
9How do you define a function type?Specifying types for parameters and the return valuefunction add(a: number, b: number): number { return a + b; }
10What is the any type?Allows a variable to hold a value of any type (use sparingly)let data: any = "anything"; data = 123;
11What is the void type?Indicates a function does not explicitly return a valuefunction logMessage(): void { console.log("Logged"); }
12What is the unknown type?Similar to any, but requires type checking before uselet value: unknown = "test"; if (typeof value === 'string') { console.log(value.toUpperCase()); }
13What is a union type ( | )?Allows a variable to hold a value of several different typeslet id: string | number = "abc"; id = 123;
14What is an intersection type (&)?Combines multiple types into a single typetype Person = { name: string }; type Employee = { id: number }; type FullTime = Person & Employee;
15How do you compile TypeScript code?Using the tsc command (TypeScript Compiler)tsc myFile.ts

Intermediate TypeScript Answers

#QuestionAnswerExamples
1Explain the difference between interface and type.interface is primarily for object shapes; type can define object shapes, unions, primitives, etc.interface User { name: string; }, type Point = { x: number; }, `type ID = string
2What are Generics?Allow you to write components that can work with a variety of types without losing type safetyfunction identity(arg: T): T { return arg; }, let output = identity("myString");
3What is an Enum?Defines a set of named constantsenum Color { Red, Green, Blue } let myColor: Color = Color.Green;
4What is a Tuple?An array with a fixed number of elements and known types for each elementlet coordinate: [number, number] = [10, 20];
5What is a Class in TypeScript?A blueprint for creating objects with properties and methodsclass Dog { name: string; constructor(name: string) { this.name = name; } bark() { console.log("Woof!"); } }
6What are access modifiers (public, private, protected)?Control the visibility of class membersclass Animal { public name: string; private secret: string; protected species: string = "unknown"; }
7What is inheritance in TypeScript?Allows a class to inherit properties and methods from another class (extends)class Cat extends Animal { meow() { console.log("Meow!"); } }
8What is an abstract class?A class that cannot be instantiated directly, serves as a base for other classesabstract class Shape { abstract draw(): void; } class Circle extends Shape { draw() { console.log("Drawing Circle"); } }
9What is a Type Assertion?Tells TypeScript to treat a value as a specific type (use with caution)let myInput = document.getElementById("myInput") as HTMLInputElement;
10What is a Type Guard?Used to narrow down the type of a variable at runtimefunction processValue(value: string | number) { if (typeof value === 'string') { console.log(value.toUpperCase()); } }
11How do you make a property optional in an object interface/type?Using a question mark (?) after the property nameinterface Config { url: string; timeout?: number; }
12What is the readonly modifier?Prevents a property from being reassigned after initializationclass Point { readonly x: number; constructor(x: number) { this.x = x; } }
13What is a Type Alias?Creates a new name for an existing typetype Coordinates = { x: number; y: number; }; let point: Coordinates = { x: 10, y: 20 };
14What is a literal type?Defines a type based on a specific literal value (string, number, boolean)type Status = "pending" | "processing" | "completed"; let status: Status = "pending";
15What are utility types?Built-in types for common type transformations (e.g., Partial, Required, Pick, Omit)let partialUser: Partial = { name: "Jane" };

Advanced TypeScript Answers

#QuestionAnswerExamples
1Explain Decorators.A way to add metadata or behavior to classes, methods, properties, or parameters@Component({}) class MyClass { ... }, @logMethod() function myMethod() { ... }
2What are Conditional Types?Allows you to create a type based on a condition (similar to a ternary operator)type IsString = T extends string ? true : false; let isStr: IsString = true; let isNotStr: IsString = false;
3What are Mapped Types?Creates a new type by iterating over the properties of an existing typetype Readonly = { readonly [P in keyof T]: T[P]; }; let ro: Readonly = { name: "Frank", age: 50 };
4What is the keyof operator?Gets a union of the property names of an object typetype UserKeys = keyof User; (UserKeys is "name" | "age")
5What is the typeof operator?Gets the type of a variable or expressionlet name = "Grace"; type NameType = typeof name; (NameType is string)
6What is the infer keyword?Used within conditional types to infer a type from a part of a typetype GetReturnType = T extends (...args: any[]) => infer R ? R : any;
7Explain TypeScript Module Resolution.How TypeScript finds module files when you importsInvolves baseUrl, paths, moduleResolution compiler options in tsconfig.json
8What is tsconfig.json?Configuration file for the TypeScript compiler (tsc)Specifies compiler options like target, module, outDir, rootDir, strict
9What is the strict flag in tsconfig.json?Enables a set of stricter type-checking optionsstrict: true enables noNullChecks, noImplicitAny, strictFunctionTypes, etc.
10What are Type Declaration Files (.d.ts)?Files that provide type definitions for JavaScript libraries that don't have TypeScript typesdeclare module 'lodash';, declare function jQuery(selector: string): any;
11How do you install types for a JavaScript library?Using @types/library-name via npm or yarnnpm install --save-dev @types/react, then import * as React from 'react';
12What is type inference?The process where TypeScript automatically determines the type of a variable based on its initial valuelet message = "Hello"; (TypeScript infers message is string)
13What is noNullChecks?Requires explicit checks for null and undefined valueslet name: string | null = null; if (name !== null) { ... } (prevents runtime errors)
14What is noImplicitAny?Prevents TypeScript from implicitly using the any type when a type cannot be inferredlet value; (causes an error if noImplicitAny is true)
15Explain the concept of namespaces in TypeScript.A way to organize code and provide global scope for sharing code across filesnamespace Utils { export class Helper { ... } }, import helper = Utils.Helper;

Last updated on July 29, 2025

🔍 Explore More Topics

Discover related content that might interest you

TwoAnswers Logo

Providing innovative solutions and exceptional experiences. Building the future.

© 2025 TwoAnswers.com. All rights reserved.

Made with by the TwoAnswers.com team