1- import { printTree } from 'tree-dump/lib/printTree' ;
1+ import { ModuleType } from '../type/classes/ModuleType' ;
2+ import { Value } from './Value' ;
3+ import { FnValue } from './FnValue' ;
24import type { Printable } from 'tree-dump/lib/types' ;
35import type * as classes from '../type' ;
46import type { TypeBuilder } from '../type/TypeBuilder' ;
5- import { ModuleType } from '../type/classes/ModuleType' ;
6- import { Value } from './Value' ;
77
88export type UnObjType < T > = T extends classes . ObjType < infer U > ? U : never ;
99export type UnObjValue < T > = T extends ObjValue < infer U > ? U : never ;
@@ -14,6 +14,8 @@ export type ObjValueToTypeMap<F> = ToObject<{
1414 [ K in keyof F ] : ObjFieldToTuple < F [ K ] > ;
1515} > ;
1616
17+ export type Ensure < T , X > = T extends X ? T : X ;
18+
1719export class ObjValue < T extends classes . ObjType < any > > extends Value < T > implements Printable {
1820 public static new = ( system : ModuleType = new ModuleType ( ) ) => new ObjValue ( { } , system . t . obj ) ;
1921
@@ -41,6 +43,18 @@ export class ObjValue<T extends classes.ObjType<any>> extends Value<T> implement
4143 return new Value ( data , field . val ) as any ;
4244 }
4345
46+ public fn < K extends keyof ObjValueToTypeMap < UnObjType < T > > > (
47+ key : K ,
48+ ) : FnValue <
49+ Ensure <
50+ ObjValueToTypeMap < UnObjType < T > > [ K ] extends classes . Type ? ObjValueToTypeMap < UnObjType < T > > [ K ] : classes . Type ,
51+ classes . FnType < any , any , any >
52+ >
53+ > {
54+ const val = this . get ( key ) ;
55+ return new FnValue ( val . data , val . type as any ) ;
56+ }
57+
4458 public field < F extends classes . KeyType < any , any > > (
4559 field : F | ( ( t : TypeBuilder ) => F ) ,
4660 data : classes . ResolveType < UnObjFieldTypeVal < F > > ,
@@ -78,7 +92,7 @@ export class ObjValue<T extends classes.ObjType<any>> extends Value<T> implement
7892 return this as any ;
7993 }
8094
81- public toString ( tab : string = '' ) : string {
82- return 'ObjValue' + printTree ( tab , [ ( tab ) => this . type ! . toString ( tab ) ] ) ;
95+ public name ( ) : string {
96+ return 'ObjValue' ;
8397 }
8498}
0 commit comments