# TypeScript

* Use "good" TypeScript practices:
  * assign a type when declaring variables (if type is not already correctly inferred)&#x20;
    * ❌ `const doc = {...} // no type; can have any fields/values`
    * ✅ `const doc: TDoc = {...} // type = TDoc ({ id: number, ... })`
  * narrow-scoped types when possible [\[1\]](https://www.allthingstypescript.dev/p/always-prefer-type-with-a-narrower)
    * ❌  `type side = string`
      * allows "left", "right", "askdjf", "jdklfjal", ...
    * ✅ `type side = "left" | "right"`
      * allows "left" or "right" only
  * minimal-to-no `any` or `unknown` types [\[1\]](https://www.allthingstypescript.dev/p/why-avoid-the-any-type-in-typescript)
    * ❌ `const item: any = {...}`
    * ❌ `const item: unknown = {...}`
  * minimal "type casting" / "type assertions" [\[1\]](https://www.w3schools.com/typescript/typescript_casting.php)[\[2\]](https://www.reddit.com/r/typescript/comments/wd3f7j/should_i_avoid_casting_types/)[\[3\]](https://www.allthingstypescript.dev/p/avoid-using-type-assertions-in-typescript)
    * ❌ `const item = {...} as TypeX`
