Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kishimoto96/de3c668e440ff7306782433afeae7419 to your computer and use it in GitHub Desktop.
Save Kishimoto96/de3c668e440ff7306782433afeae7419 to your computer and use it in GitHub Desktop.
Discussion for Typescript

Typescript discussion

Write your answers in the comment section below:

  1. What are the primitive types in TypeScript?
  2. Explain how the arrays work in TypeScript.
  3. What is any type, and should we use it?
  4. How does enums work and how are they usefull?
  5. What is optional chaining and how does it work?
  6. "Javascript is dynamically typed" what does that mean? and how does Typescript affect that?
  7. What is the difference between Interface and Class?
  8. What is union and intersection type?
  9. Why to even bother with Typescript???
@Rsmk-code
Copy link

@omikay @Rsmk-code @Abdulsalam Hanandoush @talal Bakkour

  1. In TypeScript, the primitive types include:
    number: Represents both integer and floating-point numbers.
    string: Represents textual data and is enclosed in single or double quotes.
    boolean: Represents a logical value, either true or false.
    null: Represents the absence of any object value.
    undefined: Represents an uninitialized variable.
    symbol: Represents a unique identifier.

  2. Arrays in TypeScript allow you to store multiple values of the same type. Arrays can be initialized using square brackets and can be accessed using zero-based indexing. TypeScript provides various array methods and properties to manipulate and work with arrays, such as push(), pop(), length, etc.

  3. The any type in TypeScript allows you to opt out of type checking for a particular variable or value. When a variable is assigned the any type, it essentially tells the TypeScript compiler to treat that value as having any type. While any provides flexibility, it bypasses type checking and can lead to potential runtime errors. It is generally recommended to avoid using any unless necessary.

  4. Enums in TypeScript allow a developer to define a set of named constants:
    With enums, you can create constants that you can easily relate to, making constants more legible.
    Developers have the freedom to create memory-efficient custom constants in JavaScript. JavaScript does not support enums, but TypeScript helps us access them.
    TypeScript enums save runtime and compile time with inline code in JavaScript.

enum Color {
  Red,
  Green,
  Blue
}
let myColor: Color = Color.Red;
  1. Optional chaining is a feature introduced in TypeScript that simplifies the process of accessing properties or calling methods on objects that may be null or undefined. It allows you to chain multiple property or method accesses with a question mark (?.) in between. If any part of the chain is null or undefined, the expression will short-circuit and evaluate to undefined. This helps avoid runtime errors when working with potentially nullable values.
interface Address {
  street: string;
  city?: string;
  country?: string;
}

interface Person {
  name: string;
  age: number;
  address?: Address;
}

const person: Person = {
  name: "John Doe",
  age: 25,
  // address is optional and not provided
};

// Accessing property using optional chaining
const city = person.address?.city;
console.log(city); // Output: undefined

// Accessing nested property using optional chaining
const country = person.address?.country;
console.log(country); // Output: undefined

// Calling method using optional chaining
const streetLength = person.address?.street.length;
console.log(streetLength); // Output: undefined
  1. Javascript being dynamically typed means that variable types are determined at runtime. Typescript, on the other hand, is a statically typed superset of Javascript that adds type annotations and allows for type checking at compile-time.

  2. An interface is a blueprint for defining the structure and behavior of an object, while a class is a template for creating objects with specific properties and methods.

  3. A union type combines multiple types into one, allowing a value to be of different types. An intersection type combines multiple types into one, representing a value that has all the properties of each type.

9.Typescript offers static type checking, which helps catch errors during development, improves code quality, and enhances tooling support like autocompletion and refactoring. It provides better code maintainability, scalability, and facilitates collaboration in larger codebases. Additionally, Typescript adds features like interfaces, enums, and decorators that enhance the development experience compared to plain JavaScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment