### Back to the backend!  -- - Install TS in the backend project - Create a `tsconfig.json` that extends from appropriate base(s) - Rename the `.js` files to `.ts` - Enable the `noEmit` option in `tsconfig.json` - Run `tsc` --  -- ### Types for non-TS deps [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) FTW! Community-maintained, npm-installable type declarations for popular JS packages that don't ship their own -- e.g. to install types for Node: ```sh npm i -D @types/node ``` -- ### Install all the types!  -- Install missing `@types/` packages for package dependencies `npm i -D @types/
` e.g. for `express`: `@types/express` -- Define the missing custom types to fix the errors! -- ### Type assertions with `as` ```ts const maybeAnyElement = document.getElementById('my-button'); const iSwearThisBtnExists = possibleHTMLElement as HTMLButtonElement; ``` -- 
### Sharing types -- ### Type-only imports in `.ts` files ```ts import { actualValue, type JustType } from 'values.ts'; ``` ```ts import type { OneType, AnotherType } from 'types.js' ``` -- ### Exporting types for reuse ```ts // bananas.ts export type Banana = 'banana'; export type Plantain = 'plantain'; ``` ```ts // apples.ts type Apple = 'pink lady' | 'red delicious'; const myApples: Apple[] = []; export { myApples, type Apple }; ``` -- ```ts // fruits.ts import type { Banana, Plantain } from 'bananas'; import { myApples, type Apple } from 'apples'; const fruit: Banana | Apple = 'banana'; ``` --
### Type Safety  -- ### "end-to-end type safety"  -- 
# TS OSS FTW! -- ### Types & Databases # [TypeORM](https://typeorm.io/) -- ### Data Validation Using TS types [zod.dev](https://zod.dev) _"TS-first schema validation with static type inference"_ -- - Provides typed objects to define data schemas - Checks that data has that shape at _runtime_ in JS - Lets TS infer corresponding type(s) at _compile time_ -- ### Declaring a Schema ```ts import { z } from "zod"; const User = z.object({ username: z.string(), }); ``` -- ### Validating data ```ts // throws an error if the data // doesn't match the schema User.parse({ username: "Ludwig" }); ``` -- ### Inferring TS type(s) ```ts // extract the inferred type type User = z.infer
; // { username: string } ``` --
## Type-aware Tests [vitest.dev/guide/testing-types](https://vitest.dev/guide/testing-types) ```sh npm i -D vitest ``` -- ```ts import { expectTypeOf } from 'vitest'; import myFn from './myFunction'; import MyType from './sharedSchema'; test('my types work properly', () => { expectTypeOf(myFn).toBeFunction(); expectTypeOf(myFn).not.toBeAny(); expectTypeOf(myFn).parameter(0).toMatchTypeOf
(); }); ``` -- ### Tons of helpful TS libs out there! These are just some popular examples Check out [github.com/dzharii/awesome-typescript](https://github.com/dzharii/awesome-typescript)