TypeScript discrete notes

Anh-Thi Dinh
Documentation
Inheritance and extends (Official doc)
A class can reused the properties and methods of the base class.
protected, private, public, readonly, static
  • private = can be accessed only within the class and even their sub-classes won't be allowed to use their private properties and attributes
  • public = By default, all members of a class in TypeScript are public. All the public members can be accessed anywhere without any restrictions.
type vs interface (official doc)
  • Very similar, some cases → choose one of 2 → the same!
  • Almost all features in interface are available in type
  • Key distinction: type cannot be re-opened to add new properties vs interface is extendable!
any vs unknown (Source)
Get types from arrays
Other ways → using enum 👇 Source (there are more in that page)
Type T
<T> (a type variable) → Generics / type variable : nếu we use any, chúng ta không biết function returns type gì?
This T allows us to capture the type the user provides → use this info later!
Union types (varName: A | B)
Lưu ý là AB không thể ở dạng boolean | object được! Chúng có thể là {a:boolean} | {x: string}
key in, keyof
constructor
Both will produce the same thing,
Static member (official doc)
Troubleshooting