Skip to content

Instantly share code, notes, and snippets.

@halitbatur
Created June 28, 2022 11:17
Show Gist options
  • Save halitbatur/d9aee04b6f0e75c85966c74ba26db0a4 to your computer and use it in GitHub Desktop.
Save halitbatur/d9aee04b6f0e75c85966c74ba26db0a4 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???
@khaldarov
Copy link

khaldarov commented Jun 28, 2022

Noor Awied, Lara Gurol, Adnan Khaldar

What are the primitive types in TypeScript?

​​Number, string, tuple, enum, unknown, any, void, never, boolean, null, and undefined types

Explain how the arrays work in TypeScript.

Generic type: let fruits: Array<string>
Specific type: let fruits: string[ ]
For arrays with more than one type: let values: Array<string | number>

What is any type, and should we use it?

It allows you to store a value of any type. any type is used when we deal with third-party programs and expect any variable, but we don’t know the exact type of variable.
Since we typed any data type to temp, it expects all types of data-type and doesn’t give any error.

How does enums work, and how are they useful?

Although enum is a reserved word in JavaScript, JavaScript has no support for traditional enums. However, it is fairly easy to define enums using objects in JavaScript. For example, TypeScript has support for enums:

enum Direction { Up, Down, Left, Right }

At runtime, TypeScript compiles the above code into the below enum-like object:

const Direction = { Up: 'Up', Down: 'Down', Left: 'Left', Right: 'Right' };

This object has most of the features that you would expect from an enum:
Get all allowed enum values: Object.keys(Direction) returns an array ['Up', 'Down', 'Left', 'Right']
Check if a value equals an enum value: val === Direction.Up
Check if a value is in the enum: Direction.hasOwnProperty('Up')
However, there are a couple of limitations:
You can modify the enum after instantiation. For example, Direction.sideways = 'sideways'.
If val === undefined, then val === Direction.notAnEnumValue and val === Direction.Downe. So typos in enum properties can cause issues.
No guarantee that property values don't conflict. Direction.Down = 'Up' is valid.

What is optional chaining, and how does it work?

The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.
Ex: const dogName = adventurer.dog?.name;
The optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that a reference or function may be undefined or null.

"JavaScript is dynamically typed" what does that mean, and how does Typescript affect that?

TypeScript offers a flexible and forgiving environment for writing code and script, and it’s a lightweight language, so you won’t get bogged down by excess syntax and features, and you don't know the type of your variables until run-time, so you may encounter errors or problems. When Typescript is used properly, it fixes bugs caused by false assumptions about variable types in JavaScript.

What is the difference between Interface and Class?

Classes are the fundamental entities used to create reusable components. It is a group of objects which have common properties. It can contain properties like fields, methods, constructors, etc. We use classes as object factories. A class defines a blueprint of what an object should look like and act like, and then implements that blueprint by initializing class properties and defining methods. Therefore, when we create an instance of the class, we get an object that has actionable functions and defined properties.
An Interface defines a structure which acts as a contract in our application. An interface is simply a structural contract that defines what the properties of an object should have as a name and as a type.

What is union and intersection type?

An intersection type combines multiple types into one. A union type describes a value that can be one of several types. Usage and notation are quite simple, the & symbol is used to build an intersection while the | symbol is for union.

Why to even bother with Typescript???

Advanced type system (Optional static typing)

TypeScript could be used for detecting errors before runtime, making our projects less prone to errors.

Readability

Thanks to the addition of strict types and other elements that make the code more self-expressive, you can see the design intent of the developers who originally wrote the code.

Typing support

Type information renders editors and IDEs much more useful. They can offer features like code navigation and auto-complete, providing accurate suggestions.

The power of object-orientation

TypeScript supports Object-Oriented Programming (OOP) concepts such as classes, interfaces, inheritance, etc.

ECMAScript defines the standards and novelties of JavaScript

TypeScript takes great care to include all these new features with each update.

References:

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