11// With many, many inspiration from https://github.com/badrap/valita MIT License as of 2024-09-10
22
3- import { isBoolean , isFunction , isInteger , isNumber , isObject , isString } from '../data/is'
3+ import { isArray , isBoolean , isFunction , isInteger , isNumber , isObject , isString } from '../data/is'
44import { first } from '../data/utils'
55
66export interface TypeProps {
@@ -253,10 +253,20 @@ export function tuple<T extends [] | [Type, ...Type[]]>(items: T): ArrayType<T,
253253 } )
254254}
255255
256+ export class TypeArrayClass < T , TT > extends TypeClass < T > {
257+ constructor (
258+ name : string ,
259+ type : TT ,
260+ ) {
261+ super ( name , isArray )
262+ this . _type = type
263+ }
264+
265+ _type ?: TT
266+ }
267+
256268export function array < T > ( itemType : Type < T > ) : Type < T [ ] > {
257- return generic < T [ ] > ( 'array' , {
258- _check : v => Array . isArray ( v ) && v . every ( item => itemType . _check ( item ) ) ,
259- } )
269+ return new TypeArrayClass < T [ ] , Type < T > > ( 'array' , itemType )
260270}
261271
262272// const tt = tuple([number(), string(), boolean()])
@@ -287,7 +297,7 @@ export function func<
287297 return new TypeFuncClass < T , Args , Ret > ( 'function' , args , ret )
288298}
289299
290- class TypeRpcClass < T , Info , Ret > extends TypeClass < T > {
300+ export class TypeRpcClass < T , Info , Ret > extends TypeClass < T > {
291301 constructor (
292302 name : string ,
293303 info ?: Info ,
@@ -304,9 +314,9 @@ class TypeRpcClass<T, Info, Ret> extends TypeClass<T> {
304314
305315export function rpc <
306316 Info extends Type < unknown > | undefined = undefined ,
307- Ret extends Type < unknown > = ReturnType < typeof none > ,
308- T = Info extends undefined ? ( ) => Infer < Ret > : ( info : Infer < Info > ) => Infer < Ret > ,
309- > ( info ?: Info , ret ?: Ret ) : Type < T > {
317+ Ret extends Type < unknown > = Type < void > , // ReturnType<typeof none>,
318+ T = Info extends undefined ? ( ) => Infer < Ret > : ( info : Infer < Info > ) => Infer < Ret > | Promise < Infer < Ret > > ,
319+ > ( info ?: Info , ret ?: Ret ) {
310320 return new TypeRpcClass < T , Info , Ret > ( 'rpc' , info , ret ?? none ( ) as Ret )
311321}
312322
0 commit comments