# TypeScript
## Type System
> [!summary] Philosophy
>
> Having useful error information, if errors are about to happen, make them as
> close to the original place that caused the issue as possible, except only in
> the call sites.
- Structural instead of nominal
- Interfaces are open and can be augmented, this change will take place
globally.
- Interfaces can be implemented and extended. Classes can also be implemented
(the static side of it)
- Interface registry pattern with `module` declaration.
- `void` type
- In function decoration, it means "return nothing" and must be satisfied
- In `type` definition, it means "I don't care about the return"
- Function overloading is possible with function heads
- `static` block for class setup
- Private fields can be shared among instances, `#prop` prop name
- `constructor(public name: string, ...)` syntactic sugar
- `override` keyword for overriding function in base class
- Type guards and narrowing. `instanceof`, `typeof`.
- `function isCarLike(val: any): val is CarLike`, which returns a `boolean`
for test results.
- `function isCarLike(val: any): asserts val is CarLike`, which should
`throw new Error()` if the test is not passed
- Narrowing in switch: `switch(true): case val instance of Bird: ...`
## Resources
- [TypeScript](https://www.typescriptlang.org)
- [Learn TypeScript w/ Mike North](https://typescript-training.com)
## Ecosystem
- [Zod](https://zod.dev/), "TypeScript-first schema validation with static type
inference", good type support, plus zero-dependencies.
- [Yup](https://github.com/jquense/yup)
- [Ajv](https://ajv.js.org/), [[json|JSON]] schema validator
- [Valibot](https://valibot.dev/), an alternative to Zod that is extremely light weight, due to modular design.
- [VeeValidate](https://vee-validate.logaretm.com/v4/), for [[vue|Vue]] form validation.
- [Joi](https://joi.dev/)
## Notes
- `interface` is extensible, compare this with `type`