typelevel-ts
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -15,2 +15,8 @@ # Changelog | ||
# 0.2.0 | ||
- **Breaking Change** | ||
- complete refactoring | ||
- upgrade to TypeScript 2.5.2 (@gcanti) | ||
# 0.1.3 | ||
@@ -17,0 +23,0 @@ |
@@ -1,120 +0,118 @@ | ||
export declare type Bool = 'true' | 'false'; | ||
export declare type Not<B extends Bool> = { | ||
true: 'false'; | ||
false: 'true'; | ||
}[B]; | ||
export declare type And<B1 extends Bool, B2 extends Bool> = { | ||
false: 'false'; | ||
true: { | ||
false: 'false'; | ||
true: 'true'; | ||
}[B2]; | ||
}[B1]; | ||
export declare type Or<B1 extends Bool, B2 extends Bool> = Not<And<Not<B1>, Not<B2>>>; | ||
export declare type Eq<B1 extends Bool, B2 extends Bool> = { | ||
true: { | ||
true: 'true'; | ||
false: 'false'; | ||
}; | ||
false: { | ||
true: 'false'; | ||
false: 'true'; | ||
}; | ||
}[B1][B2]; | ||
export declare type Increment = { | ||
0: '1'; | ||
1: '2'; | ||
2: '3'; | ||
3: '4'; | ||
4: '5'; | ||
5: '6'; | ||
6: '7'; | ||
7: '8'; | ||
8: '9'; | ||
9: '10'; | ||
10: never; | ||
}; | ||
export declare type StringToNumber = { | ||
0: 0; | ||
1: 1; | ||
2: 2; | ||
3: 3; | ||
4: 4; | ||
5: 5; | ||
6: 6; | ||
7: 7; | ||
8: 8; | ||
9: 9; | ||
10: 10; | ||
}; | ||
export declare type Bool = 'T' | 'F'; | ||
export declare type If<B extends Bool, Then, Else> = { | ||
true: Then; | ||
false: Else; | ||
T: Then; | ||
F: Else; | ||
}[B]; | ||
export declare type TOption<A> = TNone | TSome<A>; | ||
export interface TNone { | ||
isNone: 'true'; | ||
_A: any; | ||
export declare type Not<B extends Bool> = If<B, 'F', 'T'>; | ||
export declare type And<B1 extends Bool, B2 extends Bool> = If<B1, B2, 'F'>; | ||
export declare type Or<B1 extends Bool, B2 extends Bool> = If<B1, 'T', B2>; | ||
export declare type BoolEq<B1 extends Bool, B2 extends Bool> = If<B1, B2, Not<B2>>; | ||
export declare type Option<A> = None | Some<A>; | ||
export interface None { | ||
isNone: 'T'; | ||
_A: never; | ||
} | ||
export interface TSome<A> { | ||
isNone: 'false'; | ||
export interface Some<A> { | ||
isNone: 'F'; | ||
_A: A; | ||
} | ||
export declare type IsNone<O extends TOption<any>> = O['isNone']; | ||
export declare type IsSome<O extends TOption<any>> = Not<IsNone<O>>; | ||
export declare type TOptionUnsafeGet<O extends TOption<any>> = O['_A']; | ||
export declare type TOptionGetOrElse<O extends TOption<any>, A> = If<IsNone<O>, A, TOptionUnsafeGet<O>>; | ||
export interface Nat { | ||
prev?: any; | ||
isZero: Bool; | ||
export declare type IsNone<O extends Option<any>> = O['isNone']; | ||
export declare type IsSome<O extends Option<any>> = Not<IsNone<O>>; | ||
export declare type OptionUnsafeGet<O extends Option<any>> = O['_A']; | ||
export declare type OptionGetOrElse<O extends Option<any>, A> = If<IsNone<O>, A, OptionUnsafeGet<O>>; | ||
export interface Zero { | ||
isZero: 'T'; | ||
prev: never; | ||
} | ||
export interface Positive { | ||
prev: Positive | _0; | ||
isZero: 'false'; | ||
export interface Succ<N extends Nat> { | ||
isZero: 'F'; | ||
prev: N; | ||
} | ||
export declare type _0 = { | ||
isZero: 'true'; | ||
}; | ||
export declare type _1 = Succ<_0>; | ||
export declare type _2 = Succ<_1>; | ||
export declare type _3 = Succ<_2>; | ||
export declare type _4 = Succ<_3>; | ||
export declare type _5 = Succ<_4>; | ||
export declare type _6 = Succ<_5>; | ||
export declare type _7 = Succ<_6>; | ||
export declare type _8 = Succ<_7>; | ||
export declare type _9 = Succ<_8>; | ||
export declare type _10 = Succ<_9>; | ||
export declare type Nat = Zero | Succ<any>; | ||
export declare type One = Succ<Zero>; | ||
export declare type Two = Succ<One>; | ||
export declare type Three = Succ<Two>; | ||
export declare type Four = Succ<Three>; | ||
export declare type Five = Succ<Four>; | ||
export declare type Six = Succ<Five>; | ||
export declare type Seven = Succ<Six>; | ||
export declare type Eight = Succ<Seven>; | ||
export declare type Nine = Succ<Eight>; | ||
export declare type Ten = Succ<Nine>; | ||
export declare type IsZero<N extends Nat> = N['isZero']; | ||
export declare type Succ<N extends Nat> = { | ||
prev: N; | ||
isZero: 'false'; | ||
}; | ||
export declare type Prev<N extends Positive> = N['prev']; | ||
export declare type Prev<N extends Nat> = N['prev']; | ||
export declare type NatEq<N1 extends Nat, N2 extends Nat> = { | ||
true: IsZero<N2>; | ||
false: { | ||
true: 'false'; | ||
false: NatEq<Prev<N1>, Prev<N2>>; | ||
}[IsZero<N2>]; | ||
T: IsZero<N2>; | ||
F: If<IsZero<N2>, 'F', NatEq<Prev<N1>, Prev<N2>>>; | ||
}[IsZero<N1>]; | ||
export declare type Add<N1 extends Nat, N2 extends Nat> = { | ||
true: N2; | ||
false: Succ<Add<Prev<N1>, N2>>; | ||
T: N2; | ||
F: Succ<Add<Prev<N1>, N2>>; | ||
}[IsZero<N1>]; | ||
export declare type Sub<N1 extends Nat, N2 extends Nat> = { | ||
true: { | ||
true: TSome<_0>; | ||
false: TNone; | ||
}[IsZero<N2>]; | ||
false: { | ||
true: TSome<N1>; | ||
false: Sub<Prev<N1>, Prev<N2>>; | ||
}[IsZero<N2>]; | ||
T: If<IsZero<N2>, Some<Zero>, None>; | ||
F: If<IsZero<N2>, Some<N1>, Sub<Prev<N1>, Prev<N2>>>; | ||
}[IsZero<N1>]; | ||
export declare type UnsafeSub<N1 extends Nat, N2 extends Nat> = { | ||
true: N1; | ||
false: UnsafeSub<Prev<N1>, Prev<N2>>; | ||
}[IsZero<N2>]; | ||
export declare type UnsafeSub<N1 extends Nat, N2 extends Nat> = OptionGetOrElse<Sub<N1, N2>, Zero>; | ||
export declare type Mult<N1 extends Nat, N2 extends Nat> = { | ||
true: _0; | ||
false: { | ||
true: N2; | ||
false: Add<N2, Mult<Prev<N1>, N2>>; | ||
}[IsZero<Prev<N1>>]; | ||
T: Zero; | ||
F: If<IsZero<Prev<N1>>, N2, Add<N2, Mult<Prev<N1>, N2>>>; | ||
}[IsZero<N1>]; | ||
export declare type Lte<N1 extends Nat, N2 extends Nat> = IsSome<Sub<N2, N1>>; | ||
export declare type Lte<N1 extends Nat, N2 extends Nat> = { | ||
T: 'T'; | ||
F: If<IsZero<N2>, 'F', Lte<Prev<N1>, Prev<N2>>>; | ||
}[IsZero<N1>]; | ||
export declare type Lt<N1 extends Nat, N2 extends Nat> = And<Lte<N1, N2>, Not<NatEq<N1, N2>>>; | ||
export declare type Gte<N1 extends Nat, N2 extends Nat> = Not<Lt<N1, N2>>; | ||
export declare type Gt<N1 extends Nat, N2 extends Nat> = Not<Lte<N1, N2>>; | ||
export declare type Mod<N1 extends Nat, N2 extends Nat, R = _0> = { | ||
true: R; | ||
false: Mod<N1, N2, UnsafeSub<N1, N2>>; | ||
export declare type Mod<N1 extends Nat, N2 extends Nat, R = Zero> = { | ||
T: R; | ||
F: Mod<N1, N2, UnsafeSub<N1, N2>>; | ||
}[IsZero<N1>]; | ||
export declare type Min<N1 extends Nat, N2 extends Nat> = If<Lte<N1, N2>, N1, N2>; | ||
export declare type Max<N1 extends Nat, N2 extends Nat> = If<Lte<N1, N2>, N2, N1>; | ||
/** | ||
* max number = 10 | ||
* examples: | ||
* t.NumberToNat[0] == _0 | ||
* t.NumberToNat[1] == _1 | ||
* t.NumberToNat[10] == _10 | ||
*/ | ||
export declare type NumberToNat = TupleToObject<[_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10]>; | ||
/** max Nat = _10 */ | ||
export declare type NatToNumber<N extends Nat> = If<NatEq<N, _0>, 0, If<NatEq<N, _1>, 1, If<NatEq<N, _2>, 2, If<NatEq<N, _3>, 3, If<NatEq<N, _4>, 4, If<NatEq<N, _5>, 5, If<NatEq<N, _6>, 6, If<NatEq<N, _7>, 7, If<NatEq<N, _8>, 8, If<NatEq<N, _9>, 9, 10>>>>>>>>>>; | ||
export declare type NatToString<N extends Nat, I extends keyof Increment = '0'> = { | ||
T: I; | ||
F: NatToString<Prev<N>, Increment[I]>; | ||
}[IsZero<N>]; | ||
export declare type NatToNumber<N extends Nat> = StringToNumber[NatToString<N>]; | ||
export declare type StringToNat = { | ||
0: Zero; | ||
1: One; | ||
2: Two; | ||
3: Three; | ||
4: Four; | ||
5: Five; | ||
6: Six; | ||
7: Seven; | ||
8: Eight; | ||
9: Nine; | ||
10: Ten; | ||
}; | ||
export declare type StringOmit<L1 extends string, L2 extends string> = ({ | ||
@@ -130,5 +128,5 @@ [P in L1]: P; | ||
export declare type StringContains<S extends string, L extends string> = ({ | ||
[K in S]: 'true'; | ||
[K in S]: 'T'; | ||
} & { | ||
[key: string]: 'false'; | ||
[key: string]: 'F'; | ||
})[L]; | ||
@@ -143,69 +141,52 @@ export declare type ObjectHasKey<O, L extends string> = StringContains<keyof O, L>; | ||
}; | ||
export declare type THNil = { | ||
IsHNil: 'true'; | ||
}; | ||
export declare type THCons<H, T extends THList> = { | ||
IsHNil: 'false'; | ||
export interface HNil { | ||
isHNil: 'T'; | ||
head: never; | ||
tail: never; | ||
} | ||
export interface HCons<H, T extends HList> { | ||
isHNil: 'F'; | ||
head: H; | ||
tail: T; | ||
}; | ||
export declare type THList = THNil | THCons<any, any>; | ||
export declare type THListIsTHNil<L extends THList> = L['IsHNil']; | ||
export declare type THListHead<L extends THCons<any, any>> = L['head']; | ||
export declare type THListTail<L extends THCons<any, any>> = L['tail']; | ||
export declare type THListLength<L extends THList> = { | ||
true: _0; | ||
false: Succ<THListLength<THListTail<L>>>; | ||
}[THListIsTHNil<L>]; | ||
export declare type THListTypeAt<L extends THList, I extends Nat> = { | ||
true: THListHead<L>; | ||
false: THListTypeAt<THListTail<L>, Prev<I>>; | ||
}[IsZero<I>]; | ||
export declare type THListGet<L extends THList, I extends Nat> = { | ||
true: TNone; | ||
false: { | ||
true: TSome<THListHead<L>>; | ||
false: THListGet<THListTail<L>, Prev<I>>; | ||
}[IsZero<I>]; | ||
}[THListIsTHNil<L>]; | ||
export declare type UnsafeTHListGet<L extends THList, I extends Nat> = { | ||
true: THListHead<L>; | ||
false: UnsafeTHListGet<THListTail<L>, Prev<I>>; | ||
}[IsZero<I>]; | ||
export declare type THListReverse<L extends THList, Acc = THNil> = { | ||
true: Acc; | ||
false: THListReverse<THListTail<L>, THCons<THListHead<L>, Acc>>; | ||
}[THListIsTHNil<L>]; | ||
/** max length = 6 */ | ||
export declare type THListToTuple<L extends THList> = { | ||
true: []; | ||
false: { | ||
true: [THListHead<L>]; | ||
false: { | ||
true: [THListHead<L>, THListHead<THListTail<L>>]; | ||
false: { | ||
true: [THListHead<L>, THListHead<THListTail<L>>, THListHead<THListTail<THListTail<L>>>]; | ||
false: { | ||
true: [THListHead<L>, THListHead<THListTail<L>>, THListHead<THListTail<THListTail<L>>>, THListHead<THListTail<THListTail<THListTail<L>>>>]; | ||
false: { | ||
true: [THListHead<L>, THListHead<THListTail<L>>, THListHead<THListTail<THListTail<L>>>, THListHead<THListTail<THListTail<THListTail<L>>>>, THListHead<THListTail<THListTail<THListTail<THListTail<L>>>>>]; | ||
false: { | ||
true: [THListHead<L>, THListHead<THListTail<L>>, THListHead<THListTail<THListTail<L>>>, THListHead<THListTail<THListTail<THListTail<L>>>>, THListHead<THListTail<THListTail<THListTail<THListTail<L>>>>>, THListHead<THListTail<THListTail<THListTail<THListTail<THListTail<L>>>>>>]; | ||
false: 'Error'; | ||
}[IsZero<THListLength<THListTail<THListTail<THListTail<THListTail<THListTail<THListTail<L>>>>>>>>]; | ||
}[IsZero<THListLength<THListTail<THListTail<THListTail<THListTail<THListTail<L>>>>>>>]; | ||
}[IsZero<THListLength<THListTail<THListTail<THListTail<THListTail<L>>>>>>]; | ||
}[IsZero<THListLength<THListTail<THListTail<THListTail<L>>>>>]; | ||
}[IsZero<THListLength<THListTail<THListTail<L>>>>]; | ||
}[IsZero<THListLength<THListTail<L>>>]; | ||
}[IsZero<THListLength<L>>]; | ||
} | ||
export declare type HList = HNil | HCons<any, any>; | ||
export declare type IsHNil<L extends HList> = L['isHNil']; | ||
export declare type Head<L extends HList> = L['head']; | ||
export declare type Tail<L extends HList> = L['tail']; | ||
export declare type TypeAt<L extends HList, I extends Nat> = { | ||
T: None; | ||
F: If<IsZero<I>, Some<Head<L>>, TypeAt<Tail<L>, Prev<I>>>; | ||
}[IsHNil<L>]; | ||
export declare type UnsafeTypeAt<L extends HList, N extends Nat> = OptionGetOrElse<TypeAt<L, N>, never>; | ||
export declare type Reverse<L extends HList, Acc extends HList = HNil> = { | ||
T: Acc; | ||
F: Reverse<Tail<L>, HCons<Head<L>, Acc>>; | ||
}[IsHNil<L>]; | ||
export declare type HListLengthAsNat<L extends HList> = { | ||
T: Zero; | ||
F: Succ<HListLengthAsNat<Tail<L>>>; | ||
}[IsHNil<L>]; | ||
export declare type HListLengthAsString<L extends HList> = { | ||
T: '0'; | ||
F: Increment[HListLengthAsString<Tail<L>>]; | ||
}[IsHNil<L>]; | ||
export declare type HListLengthAsNumber<L extends HList> = StringToNumber[HListLengthAsString<L>]; | ||
export declare type HListToTuple<L extends HList> = { | ||
0: never; | ||
1: [Head<L>]; | ||
2: [Head<L>, Head<Tail<L>>]; | ||
3: [Head<L>, Head<Tail<L>>, Head<Tail<Tail<L>>>]; | ||
4: [Head<L>, Head<Tail<L>>, Head<Tail<Tail<L>>>, Head<Tail<Tail<Tail<L>>>>]; | ||
5: [Head<L>, Head<Tail<L>>, Head<Tail<Tail<L>>>, Head<Tail<Tail<Tail<L>>>>, Head<Tail<Tail<Tail<Tail<L>>>>>]; | ||
6: [Head<L>, Head<Tail<L>>, Head<Tail<Tail<L>>>, Head<Tail<Tail<Tail<L>>>>, Head<Tail<Tail<Tail<Tail<L>>>>>, Head<Tail<Tail<Tail<Tail<Tail<L>>>>>>]; | ||
}[HListLengthAsString<L>]; | ||
export declare type TupleToObject<T> = ObjectOmit<T, keyof Array<any>>; | ||
export declare type Increment = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
export declare type TupleLength<T, I = 0> = { | ||
true: TupleLength<T, Increment[I]>; | ||
false: I; | ||
export declare type TupleLengthAsString<T extends [any], I extends keyof Increment = '0'> = { | ||
T: TupleLengthAsString<T, Increment[I]>; | ||
F: I; | ||
}[ObjectHasKey<T, I>]; | ||
export declare type TupleToTHList<T, I = 0, L = THNil> = { | ||
true: TupleToTHList<T, Increment[I], THCons<T[I], L>>; | ||
false: L; | ||
export declare type TupleLengthAsNumber<T extends [any]> = StringToNumber[TupleLengthAsString<T>]; | ||
export declare type TupleToHList<T extends [any], I extends keyof Increment & keyof T = '0', L extends HList = HNil> = { | ||
T: TupleToHList<T, Increment[I], HCons<T[I], L>>; | ||
F: Reverse<L>; | ||
}[ObjectHasKey<T, I>]; |
"use strict"; | ||
// | ||
// Bool - type level booleans | ||
// helpers | ||
// | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "typelevel-ts", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Type level programming in TypeScript", | ||
@@ -31,3 +31,3 @@ "files": ["lib"], | ||
"tslint-config-standard": "4.0.0", | ||
"typescript": "2.4.1", | ||
"typescript": "^2.5.2", | ||
"typings-checker": "1.1.2" | ||
@@ -34,0 +34,0 @@ }, |
@@ -1,3 +0,8 @@ | ||
**Requires TypeScript v2.4.1+** | ||
# Compatibility | ||
- `0.2.0+` for TypeScript `2.5.2+` | ||
- `0.1.3` for TypeScript `2.4.1+` | ||
# Credits | ||
Adapted from | ||
@@ -9,13 +14,18 @@ | ||
## Related projects | ||
# `Nat`urals | ||
- [typical - playground for type-level primitives in TypeScript](https://github.com/tycho01/typical) by @tycho01 | ||
# Examples | ||
## Naturals | ||
**Example**. Type-safe vectors | ||
```ts | ||
import { _1, _2, _3, Nat, Add } from 'typelevel-ts' | ||
import { One, Two, Three, Nat, Add } from 'typelevel-ts' | ||
function create<A>(as: [A, A, A]): Vector<_3, A> | ||
function create<A>(as: [A, A]): Vector<_2, A> | ||
function create<A>(as: [A]): Vector<_1, A> | ||
function create<A>(as: [A, A, A]): Vector<Three, A> | ||
function create<A>(as: [A, A]): Vector<Two, A> | ||
function create<A>(as: [A]): Vector<One, A> | ||
function create<N extends Nat, A>(as: Array<A>): Vector<N, A> { | ||
@@ -44,7 +54,7 @@ return new Vector<N, A>(as) | ||
// v1 :: Vector<_1, number> | ||
// v1 :: Vector<One, number> | ||
const v1 = Vector.create([1]) | ||
// v2 :: Vector<_2, number> | ||
// v2 :: Vector<Two, number> | ||
const v2 = Vector.create([2, 3]) | ||
// v3 :: Vector<_3, number> | ||
// v3 :: Vector<Three, number> | ||
const v3 = v1.append(v2) | ||
@@ -57,3 +67,3 @@ | ||
# The `ObjectDiff` operator | ||
## `ObjectDiff` | ||
@@ -64,12 +74,9 @@ **Example**. A `withDefaults` function (React) | ||
import * as React from 'react' | ||
import { ObjectOmit } from 'typelevel-ts' | ||
import { ObjectDiff } from 'typelevel-ts' | ||
export default function withDefaults<D, A extends D>( | ||
C: React.ComponentType<A>, | ||
defaults: D | ||
): React.SFC<ObjectDiff<A, D>> { | ||
return (props: any) => <C {...defaults} {...props} /> | ||
function withDefaults<D, A extends D>(C: React.ComponentType<A>, defaults: D): React.SFC<ObjectDiff<A, D>> { | ||
return props => <C {...defaults} {...props} /> | ||
} | ||
class Foo extends React.Component<{ bar: string; baz: number }, void> {} | ||
class Foo extends React.Component<{ bar: string; baz: number }> {} | ||
const DefaultedFoo = withDefaults(Foo, { baz: 1 }) | ||
@@ -79,3 +86,3 @@ const x = <DefaultedFoo bar="bar" /> // ok | ||
# The `ObjectOmit` operator | ||
## `ObjectOmit` | ||
@@ -89,24 +96,9 @@ **Example**. A `withProps` function (React) | ||
function withProps<D, P extends D>(C: React.ComponentType<P>, values: D): React.SFC<ObjectOmit<P, keyof D>> { | ||
return (props: any) => <C {...props} {...values} /> | ||
return props => <C {...props} {...values} /> | ||
} | ||
class Foo extends React.Component<{ bar: string; baz: number }, void> {} | ||
class Foo extends React.Component<{ bar: string; baz: number }> {} | ||
const FilledFoo = withProps(Foo, { baz: 1 }) | ||
const x = <FilledFoo bar="bar" /> // ok | ||
``` | ||
# THlist | ||
How to return the intersection of the items | ||
```ts | ||
export type __THListIntersection<L extends THList, Acc> = { | ||
true: Acc | ||
false: __THListIntersection<THListTail<L>, THListHead<L> & Acc> | ||
}[THListIsTHNil<L>] | ||
/** returns the intersection of the contained types */ | ||
export type THListIntersection<L extends THList, WhenTHNil = never> = { | ||
true: WhenTHNil | ||
false: __THListIntersection<THListTail<L>, THListHead<L>> | ||
}[THListIsTHNil<L>] | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12723
195
99
1