Documentation
- TypeScript Deep Dive — The definitive guide to TypeScript and possibly the best TypeScript book
- Write doc as comment for functions in TypeScript → https://tsdoc.org/ → playground (interactive)
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.
- Very similar, some cases → choose one of 2 → the same!
- Almost all features in
interface
are available intype
- Key distinction:
type
cannot be re-opened to add new properties vsinterface
is extendable!
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à
A
và B
không thể ở dạng boolean | object
được! Chúng có thể là {a:boolean} | {x: string}
key in
, keyof
Troubleshooting