# Abstract Class: Arbitrary\<T\>

> Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:14](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L14)

# Abstract Class: Arbitrary\<T\>

Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:14](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L14)

Abstract class able to generate values on type `T`

The values generated by an instance of Arbitrary can be previewed - with [sample](../functions/sample.md) - or classified - with [statistics](../functions/statistics.md).

## Remarks

Since 0.0.7

## Type Parameters

| Type Parameter |
| ------ |
| `T` |

## Constructors

### Constructor

> **new Arbitrary**\<`T`\>(): `Arbitrary`\<`T`\>

#### Returns

`Arbitrary`\<`T`\>

## Methods

### canShrinkWithoutContext() {#canshrinkwithoutcontext}

> `abstract` **canShrinkWithoutContext**(`value`): `value is T`

Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:42](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L42)

Check if a given value could be pass to `shrink` without providing any context.

In general, `canShrinkWithoutContext` is not designed to be called for each `shrink` but rather on very special cases.
Its usage must be restricted to `canShrinkWithoutContext` or in the rare* contexts of a `shrink` method being called without
any context. In this ill-formed case of `shrink`, `canShrinkWithoutContext` could be used or called if needed.

*we fall in that case when fast-check is asked to shrink a value that has been provided manually by the user,
 in other words: a value not coming from a call to `generate` or a normal `shrink` with context.

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `value` | `unknown` | Value to be assessed |

#### Returns

`value is T`

`true` if and only if the value could have been generated by this instance

#### Remarks

Since 3.0.0

***

### chain() {#chain}

> **chain**\<`U`\>(`chainer`): `Arbitrary`\<`U`\>

Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:140](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L140)

Create another arbitrary by mapping a value from a base Arbirary using the provided `fmapper`
Values produced by the new arbitrary are the result of the arbitrary generated by applying `fmapper` to a value

#### Type Parameters

| Type Parameter |
| ------ |
| `U` |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `chainer` | (`t`) => `Arbitrary`\<`U`\> | Chain function, to produce a new Arbitrary using a value from another Arbitrary |

#### Returns

`Arbitrary`\<`U`\>

New arbitrary of new type

#### Example

```typescript
const arrayAndLimitArbitrary = fc.nat().chain((c: number) => fc.tuple( fc.array(fc.nat(c)), fc.constant(c)));
```

#### Remarks

Since 1.2.0

***

### filter() {#filter}

#### Call Signature

> **filter**\<`U`\>(`refinement`): `Arbitrary`\<`U`\>

Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:78](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L78)

Create another arbitrary by filtering values against `predicate`

All the values produced by the resulting arbitrary
satisfy `predicate(value) == true`

Be aware that using filter may highly impact the time required to generate a valid entry

##### Type Parameters

| Type Parameter |
| ------ |
| `U` |

##### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `refinement` | (`t`) => `t is U` | Predicate, to test each produced element. Return true to keep the element, false otherwise |

##### Returns

`Arbitrary`\<`U`\>

New arbitrary filtered using predicate

##### Example

```typescript
const integerGenerator: Arbitrary<number> = ...;
const evenIntegerGenerator: Arbitrary<number> = integerGenerator.filter(e => e % 2 === 0);
// new Arbitrary only keeps even values
```

##### Remarks

Since 1.23.0

#### Call Signature

> **filter**(`predicate`): `Arbitrary`\<`T`\>

Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:99](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L99)

Create another arbitrary by filtering values against `predicate`

All the values produced by the resulting arbitrary
satisfy `predicate(value) == true`

Be aware that using filter may highly impact the time required to generate a valid entry

##### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `predicate` | (`t`) => `boolean` | Predicate, to test each produced element. Return true to keep the element, false otherwise |

##### Returns

`Arbitrary`\<`T`\>

New arbitrary filtered using predicate

##### Example

```typescript
const integerGenerator: Arbitrary<number> = ...;
const evenIntegerGenerator: Arbitrary<number> = integerGenerator.filter(e => e % 2 === 0);
// new Arbitrary only keeps even values
```

##### Remarks

Since 0.0.1

***

### generate() {#generate}

> `abstract` **generate**(`mrng`, `biasFactor`): [`Value`](Value.md)\<`T`\>

Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:25](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L25)

Generate a value of type `T` along with its context (if any)
based on the provided random number generator

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `mrng` | [`Random`](Random.md) | Random number generator |
| `biasFactor` | `number` \| `undefined` | If taken into account 1 value over biasFactor must be biased. Either integer value greater or equal to 2 (bias) or undefined (no bias) |

#### Returns

[`Value`](Value.md)\<`T`\>

Random value of type `T` and its context

#### Remarks

Since 0.0.1 (return type changed in 3.0.0)

***

### map() {#map}

> **map**\<`U`\>(`mapper`, `unmapper?`): `Arbitrary`\<`U`\>

Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:122](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L122)

Create another arbitrary by mapping all produced values using the provided `mapper`
Values produced by the new arbitrary are the result of applying `mapper` value by value

#### Type Parameters

| Type Parameter |
| ------ |
| `U` |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `mapper` | (`t`) => `U` | Map function, to produce a new element based on an old one |
| `unmapper?` | (`possiblyU`) => `T` | Optional unmap function, it will never be used except when shrinking user defined values. Must throw if value is not compatible (since 3.0.0) |

#### Returns

`Arbitrary`\<`U`\>

New arbitrary with mapped elements

#### Example

```typescript
const rgbChannels: Arbitrary<{r:number,g:number,b:number}> = ...;
const color: Arbitrary<string> = rgbChannels.map(ch => `#${(ch.r*65536 + ch.g*256 + ch.b).toString(16).padStart(6, '0')}`);
// transform an Arbitrary producing {r,g,b} integers into an Arbitrary of '#rrggbb'
```

#### Remarks

Since 0.0.1

***

### shrink() {#shrink}

> `abstract` **shrink**(`value`, `context`): [`Stream`](Stream.md)\<[`Value`](Value.md)\<`T`\>\>

Defined in: [packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts:56](https://github.com/dubzzz/fast-check/blob/4663db899d411b24434189b8e560b5ecefc93f23/packages/fast-check/src/check/arbitrary/definition/Arbitrary.ts#L56)

Shrink a value of type `T`, may rely on the context previously provided to shrink efficiently

Must never be called with possibly invalid values and no context without ensuring that such call is legal
by calling `canShrinkWithoutContext` first on the value.

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `value` | `T` | The value to shrink |
| `context` | `unknown` | Its associated context (the one returned by generate) or `undefined` if no context but `canShrinkWithoutContext(value) === true` |

#### Returns

[`Stream`](Stream.md)\<[`Value`](Value.md)\<`T`\>\>

Stream of shrinks for value based on context (if provided)

#### Remarks

Since 3.0.0
