11import { TNonFunction , TPredicateFunction } from "./TPredicateFunction" ;
2- import { ISwitch , TDefinedSwitch } from "./ISwitch" ;
3- import { TKnown } from "./TKnown" ;
2+ import { ISwitch } from "./ISwitch" ;
3+
4+ export type Unpack < T > = T extends ( infer U ) [ ] ? U : T ;
45
56class SwitchMatched < T , K > implements ISwitch < T , K > {
67 public static for < T > ( x : T ) : any {
@@ -26,11 +27,11 @@ class SwitchMatched<T, K> implements ISwitch<T, K> {
2627 * Switch becomes a kind of Left and holds the value until it reaches the .default call. If
2728 * matching didn't happen for all cases, the value of the .default argument is returned instead.
2829 */
29- export class Switch < T , K > implements ISwitch < T , K > {
30+ export class Switch < T , K extends any [ ] > implements ISwitch < T , K > {
3031 /**
3132 * Pointer interface for lifting a value into Switch.
3233 */
33- public static for < T , K > ( x : T ) : ISwitch < T , K > {
34+ public static for < T , K extends any [ ] = [ ] > ( x : T ) : ISwitch < T , K > {
3435 return new Switch < T , K > ( x ) ;
3536 }
3637
@@ -42,14 +43,14 @@ export class Switch<T, K> implements ISwitch<T, K> {
4243 /**
4344 * Define predicate to be matched against and the value to be returned in case of matching.
4445 */
45- public case < N > ( pred : TNonFunction < T > , value : TKnown < K , N > ) : TDefinedSwitch < T , K , N > ;
46+ public case < N > ( pred : TNonFunction < T > , value : N ) : ISwitch < T , [ Unpack < K > , N ] > ;
4647
4748 /**
4849 * Define predicate function to be executed against Switch state and the value to be
4950 * returned in case of matching.
5051 */
51- public case < N > ( pred : TPredicateFunction < T > , res : TKnown < K , N > ) : TDefinedSwitch < T , K , N > ;
52- public case < N > ( pred : any , res : TKnown < K , N > ) : TDefinedSwitch < T , K , N > {
52+ public case < N > ( pred : TPredicateFunction < T > , res : N ) : ISwitch < T , [ Unpack < K > , N ] > ;
53+ public case < N > ( pred : any , res : N ) : ISwitch < T , [ Unpack < K > , N ] > {
5354 const check = typeof pred == "function" ? pred ( this . x ) : pred === this . x ;
5455
5556 return check ? SwitchMatched . for ( res ) : Switch . for ( this . x ) ;
@@ -58,7 +59,7 @@ export class Switch<T, K> implements ISwitch<T, K> {
5859 /**
5960 * Define the value to be returned in case none of the cases was matched.
6061 */
61- public default < V > ( defaultValue : V ) : K | V {
62+ public default < V > ( defaultValue : V ) : Unpack < K > | V {
6263 return defaultValue ;
6364 }
6465}
0 commit comments