-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add support of union types for arrays, tuples, objects and primi…
…tive in isExact (#345) * test: 🧪 add union type for isExact * test: 🧪 more combinations * test: 🧪 object union type * fix: 🧪 Exact Add support for primitive exact * docs: 📄 changeset * test: 🧪 object undefined union properties * test: 🧪 isExact Added more test cases for readonly object and unions * feat: 🧪 Exact * fix: 🧪 And * test: ➕ add test arrays * feat: ➕ add ArrayExact Fixed testArray for isExact * fix: 🧪 isExact * test: 🧪 testEnums * docs: 📄 changeset
- Loading branch information
Showing
4 changed files
with
266 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ts-essentials": patch | ||
--- | ||
|
||
Add support of union types for arrays, tuples, objects and primitive in `isExact` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,94 @@ | ||
export type Exact<Type, Shape> = Type extends Shape | ||
? Exclude<keyof Type, keyof Shape> extends never | ||
? Type | ||
import { AnyRecord } from "../any-record"; | ||
import { IsNever } from "../is-never"; | ||
|
||
type IsUnion<TUnion> = UnionToTuple<TUnion>["length"] extends 1 ? false : true; | ||
|
||
type UnionToFunctionInsertion<TUnion> = (TUnion extends any ? (arg: () => TUnion) => any : never) extends ( | ||
arg: infer TParam, | ||
) => any | ||
? TParam | ||
: never; | ||
|
||
type UnionToTuple<TUnion> = UnionToFunctionInsertion<TUnion> extends () => infer TReturnType | ||
? [...UnionToTuple<Exclude<TUnion, TReturnType>>, TReturnType] | ||
: []; | ||
|
||
type ExactUnionLength< | ||
TValue, | ||
TShape, | ||
TValueLength = UnionToTuple<TValue>["length"], | ||
TShapeLength = UnionToTuple<TShape>["length"], | ||
> = TValueLength extends TShapeLength ? true : false; | ||
|
||
type Xor<T, U> = T extends true ? (U extends true ? true : false) : U extends false ? true : false; | ||
|
||
type And<TTuple> = TTuple extends [infer Head, ...infer Rest] | ||
? Head extends true | ||
? And<Rest> | ||
: false | ||
: TTuple extends [] | ||
? true | ||
: false; | ||
|
||
type ObjectKeyExact<TValue, TShape> = And< | ||
[IsNever<Exclude<keyof TValue, keyof TShape>>, IsNever<Exclude<keyof TShape, keyof TValue>>] | ||
>; | ||
|
||
type ObjectValueDiff<TValue, TShape> = { | ||
[TKey in keyof TValue]: Exclude<TValue[TKey], TShape[TKey & keyof TShape]>; | ||
}[keyof TValue]; | ||
|
||
type ObjectValueExact<TValue, TShape> = And< | ||
[IsNever<ObjectValueDiff<TValue, TShape>>, IsNever<ObjectValueDiff<TShape, TValue>>] | ||
>; | ||
|
||
type ObjectExact<TValue, TShape> = [TValue] extends [TShape] | ||
? And< | ||
[ | ||
Xor<IsUnion<TValue>, IsUnion<TShape>>, | ||
ExactUnionLength<TValue, TShape>, | ||
ObjectKeyExact<TValue, TShape>, | ||
ObjectValueExact<TValue, TShape>, | ||
] | ||
> extends true | ||
? TValue | ||
: never | ||
: never; | ||
|
||
type IsArray<TValue> = [TValue] extends [readonly any[]] ? true : false; | ||
|
||
type IsReadonly<TArray> = Readonly<TArray> extends TArray ? true : false; | ||
|
||
type SameLength<TValue extends readonly any[], TShape extends readonly any[]> = IsNever< | ||
PrimitiveExact<TValue["length"], TShape["length"]> | ||
> extends true | ||
? false | ||
: true; | ||
|
||
type ArrayExact<TValue extends readonly any[], TShape extends readonly any[]> = And< | ||
[ | ||
// both arrays | ||
IsArray<TValue>, | ||
IsArray<TShape>, | ||
// same length | ||
SameLength<TValue, TShape>, | ||
// both readonly or not | ||
Xor<IsReadonly<TValue>, IsReadonly<TShape>>, | ||
] | ||
> extends true | ||
? [TValue, TShape] extends [readonly (infer TValueElement)[], readonly (infer TShapeElement)[]] | ||
? Exact<TValueElement, TShapeElement> extends TValueElement | ||
? TValue | ||
: never | ||
: never | ||
: never; | ||
|
||
type PrimitiveExact<TValue, TShape> = [TValue] extends [TShape] ? ([TShape] extends [TValue] ? TValue : never) : never; | ||
|
||
export type Exact<TValue, TShape> = [TValue] extends [readonly any[]] | ||
? [TShape] extends [readonly any[]] | ||
? ArrayExact<TValue, TShape> | ||
: never | ||
: [TValue] extends [AnyRecord] | ||
? ObjectExact<TValue, TShape> | ||
: PrimitiveExact<TValue, TShape>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { Exact } from "../../exact"; | ||
|
||
export const isExact = | ||
<Expected>() => | ||
<Actual>(actual: Exact<Actual, Expected>): Expected => { | ||
return actual; | ||
}; | ||
<ExpectedShape>() => | ||
<ActualShape>(x: Exact<ActualShape, ExpectedShape>) => | ||
x as ExpectedShape; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters