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,15 @@ 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 < ObjValueToTypeMap < UnObjType < T > > [ K ] extends classes . Type ? ObjValueToTypeMap < UnObjType < T > > [ K ] : classes . Type , classes . FnType < any , any , any > >
50+ > {
51+ const val = this . get ( key ) ;
52+ return new FnValue ( val . data , val . type as any ) ;
53+ }
54+
4455 public field < F extends classes . KeyType < any , any > > (
4556 field : F | ( ( t : TypeBuilder ) => F ) ,
4657 data : classes . ResolveType < UnObjFieldTypeVal < F > > ,
@@ -78,7 +89,7 @@ export class ObjValue<T extends classes.ObjType<any>> extends Value<T> implement
7889 return this as any ;
7990 }
8091
81- public toString ( tab : string = '' ) : string {
82- return 'ObjValue' + printTree ( tab , [ ( tab ) => this . type ! . toString ( tab ) ] ) ;
92+ public name ( ) : string {
93+ return 'ObjValue' ;
8394 }
8495}
0 commit comments