> For the complete documentation index, see [llms.txt](https://pavewise.gitbook.io/pavewise-style-guide-and-more/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pavewise.gitbook.io/pavewise-style-guide-and-more/frontend/basic-guidelines/typescript.md).

# 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`
