Showing 30 question(s)

Answer:

TypeScript is an open-source programming language developed by Microsoft that extends JavaScript by adding static typing, interfaces, classes, enums, and other features. It compiles to plain JavaScript.

Code Example:

let message: string = "Hello TypeScript";
console.log(message);

Tags:

Answer:

TypeScript provides static type checking, better IDE support, interfaces, generics, improved maintainability, compile-time error detection, and modern ECMAScript features.

Code Example:

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

Tags:

Answer:

TypeScript can be installed globally or locally using npm.

Code Example:

npm install -g typescript

tsc --version

Tags:

Answer:

Use the TypeScript compiler (tsc) to convert TypeScript into JavaScript.

Code Example:

tsc app.ts

tsc --watch

Tags:

Answer:

Type annotations specify the expected data type of variables, parameters, and return values.

Code Example:

let age: number = 25;
let name: string = "John";
let active: boolean = true;

Tags:

Answer:

Common data types include string, number, boolean, null, undefined, bigint, symbol, object, array, tuple, enum, any, unknown, never, and void.

Code Example:

let id: number = 1;
let username: string = "admin";
let isLoggedIn: boolean = true;

Tags:

Answer:

The any type disables type checking and allows a variable to hold values of any type. It should be avoided whenever possible.

Code Example:

let value: any = 100;

value = "Hello";

value = true;

Tags:

Answer:

unknown is a safer alternative to any. You must perform type checking before using an unknown value.

Code Example:

let value: unknown = "TypeScript";

if (typeof value === "string") {
  console.log(value.toUpperCase());
}

Tags:

Answer:

any bypasses all type checking, whereas unknown requires explicit type checking before accessing properties or methods.

Code Example:

let data: unknown = "Hello";

if (typeof data === "string") {
  console.log(data.length);
}

Tags:

Answer:

The never type represents values that never occur. It is commonly used for functions that always throw exceptions or never return.

Code Example:

function throwError(message: string): never {
  throw new Error(message);
}

Tags:

Answer:

An interface defines the structure or contract that an object must follow. It specifies property names, types, and method signatures without providing implementations.

Code Example:

interface Employee {
  id: number;
  name: string;
}

const emp: Employee = {
  id: 1,
  name: "John"
};

Tags:

Answer:

Interfaces are mainly used for object shapes and support declaration merging. Type aliases can represent primitives, unions, tuples, intersections, and objects but do not support declaration merging.

Code Example:

interface User {
  name: string;
}

type Product = {
  id: number;
};

Tags:

Answer:

Yes. An interface can inherit properties and methods from one or more interfaces using the extends keyword.

Code Example:

interface Person {
  name: string;
}

interface Employee extends Person {
  salary: number;
}

const emp: Employee = {
  name: "John",
  salary: 50000
};

Tags:

Answer:

A type alias creates a new name for an existing type. It can represent primitives, objects, unions, tuples, intersections, and function signatures.

Code Example:

type Employee = {
  id: number;
  name: string;
};

const emp: Employee = {
  id: 1,
  name: "Alice"
};

Tags:

Answer:

An enum is a named collection of constant values that improves code readability and maintainability.

Code Example:

enum Status {
  Pending,
  Approved,
  Rejected
}

console.log(Status.Pending);

Tags:

Answer:

Numeric enums use numeric values, while string enums assign meaningful string values to members.

Code Example:

enum Role {
  Admin = "ADMIN",
  User = "USER"
}

console.log(Role.Admin);

Tags:

Answer:

A tuple is a fixed-length array where each element has a predefined type.

Code Example:

let employee: [number, string];

employee = [1, "John"];

console.log(employee);

Tags:

Answer:

TypeScript allows defining parameter types and return types for better type safety.

Code Example:

function add(a: number, b: number): number {
  return a + b;
}

console.log(add(5, 10));

Tags:

Answer:

Optional parameters are marked using the ? symbol and are not required when calling a function.

Code Example:

function greet(name: string, city?: string) {
  console.log(name, city);
}

greet("John");
greet("John", "London");

Tags:

Answer:

Default parameters automatically assign a default value when an argument is not provided.

Code Example:

function welcome(name: string = "Guest") {
  console.log("Welcome " + name);
}

welcome();
welcome("Alice");

Tags:

Answer:

A class is a blueprint for creating objects. It supports properties, methods, constructors, inheritance, access modifiers, and other object-oriented programming features.

Code Example:

class Employee {
  constructor(public name: string) {}

  display() {
    console.log(this.name);
  }
}

const emp = new Employee("John");
emp.display();

Tags:

Answer:

Access modifiers control the visibility of class members. TypeScript provides public, private, and protected access modifiers.

Code Example:

class Person {
  public name = "John";
  private age = 25;
  protected city = "London";
}

Tags:

Answer:

public members are accessible everywhere, private members are accessible only within the class, and protected members are accessible within the class and its derived classes.

Code Example:

class Animal {
  public type = "Dog";
  private age = 5;
  protected color = "Brown";
}

Tags:

Answer:

A constructor is a special method that is automatically executed when an object is created. It initializes class properties.

Code Example:

class Student {
  constructor(public name: string, public age: number) {}
}

const s = new Student("Alice", 20);

Tags:

Answer:

Inheritance allows one class to reuse the properties and methods of another class using the extends keyword.

Code Example:

class Animal {
  speak() {
    console.log("Animal speaks");
  }
}

class Dog extends Animal {}

const dog = new Dog();
dog.speak();

Tags:

Answer:

Method overriding allows a derived class to provide its own implementation of a method defined in the base class.

Code Example:

class Animal {
  speak() {
    console.log("Animal");
  }
}

class Dog extends Animal {
  override speak() {
    console.log("Bark");
  }
}

Tags:

Answer:

Generics allow creating reusable components that work with multiple data types while maintaining type safety.

Code Example:

function identity<T>(value: T): T {
  return value;
}

console.log(identity<number>(10));
console.log(identity<string>("Hello"));

Tags:

Answer:

Generics improve code reusability, maintainability, and compile-time type safety while avoiding code duplication.

Code Example:

class Box<T> {
  constructor(public value: T) {}
}

const numberBox = new Box<number>(100);

Tags:

Answer:

Generic interfaces allow defining reusable contracts that work with different data types.

Code Example:

interface ApiResponse<T> {
  data: T;
  success: boolean;
}

const response: ApiResponse<string> = {
  data: "Success",
  success: true
};

Tags:

Answer:

Modules help organize code into reusable files. They use export to expose members and import to consume them.

Code Example:

// math.ts
export function add(a: number, b: number) {
  return a + b;
}

// app.ts
import { add } from "./math";

console.log(add(5, 3));

Tags: