Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce PickType #92

Open
wants to merge 7 commits into
base: next
Choose a base branch
from
Open
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
52 changes: 51 additions & 1 deletion etc/types.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export abstract class BaseTypeImpl<ResultType, TypeConfig = unknown> implements
literal(input: DeepUnbranded<ResultType>): ResultType;
maybeStringify(value: ResultType): string | undefined;
abstract readonly name: string;
or<Other extends BaseTypeImpl<unknown>>(_other: Other): Type<ResultType | TypeOf<Other>>;
or<Other extends BaseTypeImpl<unknown>>(_other: Other): ObjectType<ResultType | TypeOf<Other>>;
stringify(value: ResultType): string;
abstract readonly typeConfig: TypeConfig;
protected typeParser?(input: unknown, options: ValidationOptions): Result<unknown>;
Expand Down Expand Up @@ -317,6 +317,12 @@ export type MessageDetails = Partial<ValidationDetails> & {
message: string;
});

// Warning: (ae-forgotten-export) The symbol "PickableImpl" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "PickableKeys" needs to be exported by the entry point index.d.ts
//
// @public
export const narrowPickedKeys: <TypeImpl extends PickableImpl, Keys extends PickableKeys<TypeOf<TypeImpl>>>(pickedKeys: OneOrMore<Keys>, innerTypes: OneOrMore<TypeImpl>) => OneOrMore<Keys[]>;

// @public (undocumented)
export const nullType: TypeImpl<LiteralType<null>>;

Expand Down Expand Up @@ -371,6 +377,44 @@ export type PartialType<Props extends Properties> = TypeImpl<InterfaceType<Simpl
// @public (undocumented)
export function pattern<const BrandName extends string>(name: BrandName, regExp: RegExp, customMessage?: StringTypeConfig['customMessage']): Type<Branded<string, BrandName>, StringTypeConfig>;

// Warning: (ae-forgotten-export) The symbol "DistributedPick" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export function pick<TypeImpl extends PickableImpl, Keys extends PickableKeys<TypeOf<TypeImpl>>>(...args: [name: string, baseType: TypeImpl, keys: OneOrMore<Keys>] | [baseType: TypeImpl, keys: OneOrMore<Keys>]): ObjectType<DistributedPick<TypeOf<TypeImpl>, Keys>>;

// @public (undocumented)
export const pickPropertiesInfo: (propsInfo: PropertiesInfo, keys: OneOrMore<string>) => {
[k: string]: PropertyInfo<Type<any, unknown>>;
};

// @public (undocumented)
export class PickType<Type extends PickableImpl, ResultType> extends BaseObjectLikeTypeImpl<ResultType> {
constructor(type: Type, keys: OneOrMore<PickableKeys<TypeOf<Type>>>, name?: string);
// (undocumented)
accept<R>(visitor: Visitor<R>): R;
// (undocumented)
basicType: BasicType;
// (undocumented)
readonly isDefaultName: boolean;
// (undocumented)
readonly keys: OneOrMore<PickableKeys<TypeOf<Type>>>;
maybeStringify(value: ResultType): string;
// (undocumented)
readonly name: string;
// (undocumented)
possibleDiscriminators: readonly PossibleDiscriminator[];
// (undocumented)
readonly props: Properties;
// (undocumented)
propsInfo: PropertiesInfo;
// (undocumented)
readonly type: Type;
// (undocumented)
typeConfig: unknown;
// (undocumented)
protected typeValidator(input: unknown, options: ValidationOptions): Result<ResultType>;
}

// @public
export type PossibleDiscriminator = {
readonly path: readonly string[];
Expand Down Expand Up @@ -414,6 +458,9 @@ export type PropertyInfo<T extends Type<unknown> = Type<unknown>> = {
type: T;
};

// @public (undocumented)
export function propsInfoToProps(propsInfo: PropertiesInfo): Properties;

// @public
export function record<KeyType extends number | string, ValueType>(...args: [name: string, keyType: BaseTypeImpl<KeyType>, valueType: BaseTypeImpl<ValueType>, strict?: boolean] | [keyType: BaseTypeImpl<KeyType>, valueType: BaseTypeImpl<ValueType>, strict?: boolean]): TypeImpl<RecordType<BaseTypeImpl<KeyType>, KeyType, BaseTypeImpl<ValueType>, ValueType>>;

Expand Down Expand Up @@ -634,6 +681,9 @@ export type ValidationResult = boolean | string | MessageDetails | Array<string
// @public
export type Validator<ResultType> = (input: ResultType, options: ValidationOptions) => ValidationResult;

// @public
export const validPick: (pickedKeys: string[], innerTypeKeys: OneOrMore<string[]>) => boolean;

// @public (undocumented)
export function valueof<T extends Record<string, string>>(...args: [name: string, obj: T] | [obj: T]): TypeImpl<KeyofType<Transposed<T>>>;

Expand Down
4 changes: 2 additions & 2 deletions markdown/types.basetypeimpl.or.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Union this Type with another Type.
**Signature:**

```typescript
or<Other extends BaseTypeImpl<unknown>>(_other: Other): Type<ResultType | TypeOf<Other>>;
or<Other extends BaseTypeImpl<unknown>>(_other: Other): ObjectType<ResultType | TypeOf<Other>>;
```

## Parameters
Expand All @@ -20,7 +20,7 @@ or<Other extends BaseTypeImpl<unknown>>(_other: Other): Type<ResultType | TypeOf

**Returns:**

[Type](./types.type.md)<!-- -->&lt;ResultType \| [TypeOf](./types.typeof.md)<!-- -->&lt;Other&gt;&gt;
[ObjectType](./types.objecttype.md)<!-- -->&lt;ResultType \| [TypeOf](./types.typeof.md)<!-- -->&lt;Other&gt;&gt;

## Remarks

Expand Down
36 changes: 21 additions & 15 deletions markdown/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Runtime type-validation with derived TypeScript types.
| [IntersectionType](./types.intersectiontype.md) | The implementation behind types created with [intersection()](./types.intersection.md) and [BaseObjectLikeTypeImpl.and()](./types.baseobjectliketypeimpl.and.md)<!-- -->. |
| [KeyofType](./types.keyoftype.md) | The implementation behind types created with [keyof()](./types.keyof.md) and [valueof()](./types.valueof.md)<!-- -->. |
| [LiteralType](./types.literaltype.md) | The implementation behind types created with [literal()](./types.literal.md) and [nullType](./types.nulltype.md)<!-- -->, [undefinedType](./types.undefinedtype.md) and [voidType](./types.voidtype.md)<!-- -->. |
| [PickType](./types.picktype.md) | |
| [RecordType](./types.recordtype.md) | The implementation behind types created with [record()](./types.record.md)<!-- -->. |
| [SimpleType](./types.simpletype.md) | Implementation for simple types such as primitive types. |
| [UnionType](./types.uniontype.md) | The implementation behind types created with [union()](./types.union.md) and [BaseTypeImpl.or()](./types.basetypeimpl.or.md)<!-- -->. |
Expand Down Expand Up @@ -42,9 +43,11 @@ Runtime type-validation with derived TypeScript types.
| [object(args)](./types.object.md) | Create a type-validator that validates (or parses) an object structure. |
| [partial(args)](./types.partial.md) | Create a type-validator that validates (or parses) an object structure with only optional properties. |
| [pattern(name, regExp, customMessage)](./types.pattern.md) | |
| [pick(args)](./types.pick.md) | |
| [printKey(key)](./types.printkey.md) | Print a property-key in a JavaScript compatible way. |
| [printPath(path)](./types.printpath.md) | Print a property-path in a "JavaScripty way". |
| [printValue(input, budget, visited)](./types.printvalue.md) | Print an unknown value with a given character budget (default: 50). |
| [propsInfoToProps(propsInfo)](./types.propsinfotoprops.md) | |
| [record(args)](./types.record.md) | Note: record has strict validation by default, while type does not have strict validation, both are strict in construction though. TODO: document |
| [reportError_2(root, level, omitInput)](./types.reporterror_2.md) | Creates an human-readable error report of the given failure. |
| [union(args)](./types.union.md) | |
Expand All @@ -71,21 +74,24 @@ Runtime type-validation with derived TypeScript types.

## Variables

| Variable | Description |
| --------------------------------------------- | -------------------------------------------------------------------------------------------- |
| [autoCastFailure](./types.autocastfailure.md) | Returned by an autocaster to indicate that it is not able to auto-cast the given input. |
| [boolean](./types.boolean.md) | Built-in validator for boolean-values. |
| [brands](./types.brands.md) | The symbol that gives access to the (design-time-only) brands of a Type. |
| [designType](./types.designtype.md) | The symbol that gives access to the (design-time-only) associated TypeScript type of a Type. |
| [int](./types.int.md) | |
| [nullType](./types.nulltype.md) | |
| [number](./types.number.md) | |
| [string](./types.string.md) | Built-in validator for string-values. |
| [undefinedType](./types.undefinedtype.md) | |
| [unknown](./types.unknown.md) | Built-in validator that accepts all values. |
| [unknownArray](./types.unknownarray.md) | Built-in validator that accepts all arrays. |
| [unknownRecord](./types.unknownrecord.md) | Built-in validator that accepts all objects (<code>null</code> is not accepted). |
| [voidType](./types.voidtype.md) | |
| Variable | Description |
| --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [autoCastFailure](./types.autocastfailure.md) | Returned by an autocaster to indicate that it is not able to auto-cast the given input. |
| [boolean](./types.boolean.md) | Built-in validator for boolean-values. |
| [brands](./types.brands.md) | The symbol that gives access to the (design-time-only) brands of a Type. |
| [designType](./types.designtype.md) | The symbol that gives access to the (design-time-only) associated TypeScript type of a Type. |
| [int](./types.int.md) | |
| [narrowPickedKeys](./types.narrowpickedkeys.md) | Return the intersection of picked keys with each of the property keys of the inner types. |
| [nullType](./types.nulltype.md) | |
| [number](./types.number.md) | |
| [pickPropertiesInfo](./types.pickpropertiesinfo.md) | |
| [string](./types.string.md) | Built-in validator for string-values. |
| [undefinedType](./types.undefinedtype.md) | |
| [unknown](./types.unknown.md) | Built-in validator that accepts all values. |
| [unknownArray](./types.unknownarray.md) | Built-in validator that accepts all arrays. |
| [unknownRecord](./types.unknownrecord.md) | Built-in validator that accepts all objects (<code>null</code> is not accepted). |
| [validPick](./types.validpick.md) | Check if the intersection of all the non-empty narrowed keys is not empty. If it is, it means the picked keys describe an impossible union variant. Empty narrowed keys are ignored, because they will not end up in the union at all. |
| [voidType](./types.voidtype.md) | |

## Type Aliases

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

[Home](./index.md) &gt; [@skunkteam/types](./types.md) &gt; [narrowPickedKeys](./types.narrowpickedkeys.md)

## narrowPickedKeys variable

Return the intersection of picked keys with each of the property keys of the inner types.

**Signature:**

```typescript
narrowPickedKeys: <TypeImpl extends PickableImpl, Keys extends PickableKeys<TypeOf<TypeImpl>>>(
pickedKeys: OneOrMore<Keys>,
innerTypes: OneOrMore<TypeImpl>,
) => OneOrMore<Keys[]>;
```
23 changes: 23 additions & 0 deletions markdown/types.pick.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; [pick](./types.pick.md)

## pick() function

**Signature:**

```typescript
declare function pick<TypeImpl extends PickableImpl, Keys extends PickableKeys<TypeOf<TypeImpl>>>(
...args: [name: string, baseType: TypeImpl, keys: OneOrMore<Keys>] | [baseType: TypeImpl, keys: OneOrMore<Keys>]
): ObjectType<DistributedPick<TypeOf<TypeImpl>, Keys>>;
```

## Parameters

| Parameter | Type | Description |
| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| args | \[name: string, baseType: TypeImpl, keys: [OneOrMore](./types.oneormore.md)<!-- -->&lt;Keys&gt;\] \| \[baseType: TypeImpl, keys: [OneOrMore](./types.oneormore.md)<!-- -->&lt;Keys&gt;\] | |

**Returns:**

[ObjectType](./types.objecttype.md)<!-- -->&lt;DistributedPick&lt;[TypeOf](./types.typeof.md)<!-- -->&lt;TypeImpl&gt;, Keys&gt;&gt;
13 changes: 13 additions & 0 deletions markdown/types.pickpropertiesinfo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@skunkteam/types](./types.md) &gt; [pickPropertiesInfo](./types.pickpropertiesinfo.md)

## pickPropertiesInfo variable

**Signature:**

```typescript
pickPropertiesInfo: (propsInfo: PropertiesInfo, keys: OneOrMore<string>) => {
[k: string]: PropertyInfo<Type<any, unknown>>;
}
```
21 changes: 21 additions & 0 deletions markdown/types.picktype._constructor_.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@skunkteam/types](./types.md) &gt; [PickType](./types.picktype.md) &gt; [(constructor)](./types.picktype._constructor_.md)

## PickType.(constructor)

Constructs a new instance of the `PickType` class

**Signature:**

```typescript
constructor(type: Type, keys: OneOrMore<PickableKeys<TypeOf<Type>>>, name?: string);
```

## Parameters

| Parameter | Type | Description |
| --------- | -------------------------------------------------------------------------------------------------------------------- | ------------ |
| type | Type | |
| keys | [OneOrMore](./types.oneormore.md)<!-- -->&lt;PickableKeys&lt;[TypeOf](./types.typeof.md)<!-- -->&lt;Type&gt;&gt;&gt; | |
| name | string | _(Optional)_ |
Loading