Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions etc/types.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export type Branded<T, BrandName extends string> = T extends WithBrands<infer Ba
// @public
export const brands: unique symbol;

// @public
export function checkOneOrMore<T>(arr: T[]): OneOrMore<T>;

// @public
export function createType<Impl extends BaseTypeImpl<any, any>>(impl: Impl, override?: Partial<Record<keyof BaseTypeImpl<any, any> | 'typeValidator' | 'typeParser' | 'customValidators', PropertyDescriptor>>): TypeImpl<Impl>;

Expand Down Expand Up @@ -233,6 +236,9 @@ export class IntersectionType<Types extends OneOrMore<BaseObjectLikeTypeImpl<unk
protected typeValidator(input: unknown, options: ValidationOptions): Result<IntersectionOfTypeTuple<Types>>;
}

// @public
export function isOneOrMore<T>(arr: T[]): arr is OneOrMore<T>;

// @public
export function isType(value: unknown): value is Type<unknown>;

Expand Down
23 changes: 23 additions & 0 deletions markdown/types.checkoneormore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@skunkteam/types](./types.md) &gt; [checkOneOrMore](./types.checkoneormore.md)

## checkOneOrMore() function

Returns the original array if and only if it has at least one element.

**Signature:**

```typescript
declare function checkOneOrMore<T>(arr: T[]): OneOrMore<T>;
```

## Parameters

| Parameter | Type | Description |
| --------- | ----- | ----------- |
| arr | T\[\] | |

**Returns:**

[OneOrMore](./types.oneormore.md)<!-- -->&lt;T&gt;
27 changes: 27 additions & 0 deletions markdown/types.isoneormore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@skunkteam/types](./types.md) &gt; [isOneOrMore](./types.isoneormore.md)

## isOneOrMore() function

Type guard for `OneOrMore`

**Signature:**

```typescript
declare function isOneOrMore<T>(arr: T[]): arr is OneOrMore<T>;
```

## Parameters

| Parameter | Type | Description |
| --------- | ----- | ----------- |
| arr | T\[\] | |

**Returns:**

arr is [OneOrMore](./types.oneormore.md)<!-- -->&lt;T&gt;

## Remarks

This checks if the array has at least one element.
2 changes: 2 additions & 0 deletions markdown/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Runtime type-validation with derived TypeScript types.
| [autoCast(type)](./types.autocast.md) | Returns the same type, but with an auto-casting default parser installed. |
| [autoCastAll(type)](./types.autocastall.md) | Create a recursive autocasting version of the given type. |
| [booleanAutoCaster(input)](./types.booleanautocaster.md) | |
| [checkOneOrMore(arr)](./types.checkoneormore.md) | Returns the original array if and only if it has at least one element. |
| [createType(impl, override)](./types.createtype.md) | Create a Type from the given type-implementation. |
| [intersection(args)](./types.intersection.md) | Intersect the given types. |
| [isOneOrMore(arr)](./types.isoneormore.md) | Type guard for <code>OneOrMore</code> |
| [isType(value)](./types.istype.md) | Type-guard that asserts that a given value is a Type. |
| [keyof(args)](./types.keyof.md) | |
| [literal(value)](./types.literal.md) | |
Expand Down
4 changes: 4 additions & 0 deletions markdown/types.oneormore.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ An Array with at least one element.
```typescript
type OneOrMore<T> = [T, ...T[]];
```

## Remarks

Note that this type does not disable the mutable methods of the array, which may still invalidate the non-emptiness of the array.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export * from './symbols';
export * from './type-guard';
export * from './types';
export { printKey, printPath, printValue } from './utils/print-utils';
export { checkOneOrMore, isOneOrMore } from './utils/type-utils';
export * from './validation-error';
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ export type TypeguardResult<ResultType, Input> =

/**
* An Array with at least one element.
* @remarks
* Note that this type does not disable the mutable methods of the array, which may still invalidate the non-emptiness of the array.
*/
export type OneOrMore<T> = [T, ...T[]];

Expand Down
8 changes: 8 additions & 0 deletions src/utils/type-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { BasicType, Failure, OneOrMore, Result, ValidationResult } from '../interfaces';

/**
* Type guard for `OneOrMore`
* @remarks
* This checks if the array has at least one element.
*/
export function isOneOrMore<T>(arr: T[]): arr is OneOrMore<T> {
return arr.length > 0;
}

/**
* Returns the original array if and only if it has at least one element.
*/
export function checkOneOrMore<T>(arr: T[]): OneOrMore<T> {
// istanbul ignore if
if (!isOneOrMore(arr)) {
Expand Down