# Use patrun from TypeScript

Type a router's data, so that add takes only data of that type and find answers with it.

Use this to have the compiler check what a router holds. The package has its own types: give `patrun` the type of a pattern's data, and `find` answers with that type or `null`.

```ts
import patrun from 'patrun'
import type { PatrunRouter } from 'patrun'

const tax: PatrunRouter<number> = patrun<number>({ order: ['country'] })

tax.add({ country: 'IE' }, 0.25).add({ country: 'IE', type: 'food' }, 0.048)

const rate: number | null = tax.find({ country: 'IE', type: 'food' })
const rates: number[] = tax.collect({ country: 'IE' })

tax.add({ country: 'IE' }, 'high') // does not compile: the data is a number
tax.add({ country: ['IE'] }, 0.25) // does not compile: a value is not an array
```

A pattern may be an object of any type whose members are all strings, numbers, or booleans, so one typed by an interface does as well as a literal. With no type given, a router holds anything.

## What to watch for

A customiser whose function changes what `find` answers gives that as a second type: `patrun<string, string[]>(...)` holds strings and finds lists of them.

What compiles is not in every case what a function takes when it runs. A query may be any object to the types, so an array compiles, and matches nothing. The [types reference](https://rjrodger.github.io/patrun/reference/typescript.html) lists each type.
