File tree 2 files changed +20
-6
lines changed
2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import { evaluate } from './evaluate.js'
3
3
import { sleep } from '../../async/index.js'
4
4
import { asyncForAll , forAll , unknown } from '../../random/index.js'
5
5
6
- import { it } from 'vitest'
6
+ import { it , expectTypeOf } from 'vitest'
7
7
8
8
it ( 'constant' , ( ) => {
9
9
forAll ( unknown ( ) , ( x ) => evaluate ( x ) === x )
@@ -23,3 +23,20 @@ it('async fn', async () => {
23
23
)
24
24
} )
25
25
} )
26
+
27
+ it ( 'types' , ( ) => {
28
+ expectTypeOf ( evaluate ( 1 ) ) . toEqualTypeOf < 1 > ( )
29
+ expectTypeOf ( evaluate ( 'foo' ) ) . toEqualTypeOf < 'foo' > ( )
30
+ expectTypeOf ( evaluate ( true ) ) . toEqualTypeOf < true > ( )
31
+ expectTypeOf ( evaluate ( null ) ) . toEqualTypeOf < null > ( )
32
+
33
+ expectTypeOf ( evaluate ( ( ) => 1 ) ) . toEqualTypeOf < number > ( )
34
+ expectTypeOf ( evaluate ( ( ) => 'foo' ) ) . toEqualTypeOf < string > ( )
35
+ expectTypeOf ( evaluate ( ( ) => true ) ) . toEqualTypeOf < boolean > ( )
36
+ expectTypeOf ( evaluate ( ( ) => null ) ) . toEqualTypeOf < null > ( )
37
+
38
+ expectTypeOf ( evaluate ( ( ..._ : any [ ] ) => 1 ) ) . toEqualTypeOf < number > ( )
39
+ expectTypeOf ( evaluate ( ( ..._ : any [ ] ) => 'foo' ) ) . toEqualTypeOf < string > ( )
40
+ expectTypeOf ( evaluate ( ( ..._ : any [ ] ) => true ) ) . toEqualTypeOf < boolean > ( )
41
+ expectTypeOf ( evaluate ( ( ..._ : any [ ] ) => null ) ) . toEqualTypeOf < null > ( )
42
+ } )
Original file line number Diff line number Diff line change 1
1
import { isFunction } from '../../guard/is-function/index.js'
2
- import type { ConstExpr } from '../../type/function/index.js'
3
-
4
- export type Evaluated < T extends ConstExpr > = T extends ( ) => infer V ? V : T
5
2
6
3
/**
7
4
* Takes a value or a function that returns a value, and returns the value.
@@ -23,6 +20,6 @@ export type Evaluated<T extends ConstExpr> = T extends () => infer V ? V : T
23
20
*
24
21
* @group Functions
25
22
*/
26
- export function evaluate < T extends ConstExpr > ( maybeEvaluate : T ) : Evaluated < T > {
27
- return ( isFunction ( maybeEvaluate ) ? maybeEvaluate ( ) : maybeEvaluate ) as Evaluated < T >
23
+ export function evaluate < const T > ( maybeEvaluate : T | ( ( ) => T ) ) : T {
24
+ return isFunction ( maybeEvaluate ) ? maybeEvaluate ( ) : maybeEvaluate
28
25
}
You can’t perform that action at this time.
0 commit comments