Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mizdra committed Mar 21, 2024
1 parent 967d3b5 commit a14b6c0
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/helper/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ export interface DefineTypeFactoryInterfaceRequired<
> {
<
_DefaultFieldsResolver extends Required<FieldsResolver<Type & TransientFields>>,
_Traits extends Traits<Type, TransientFields> = Traits<Type, TransientFields>,
_Traits extends Traits<Type, TransientFields>,
>(
options: TypeFactoryDefineOptions<Type, TransientFields, _DefaultFieldsResolver, _Traits>,
): TypeFactoryInterface<Type, TransientFields, _DefaultFieldsResolver, _Traits>;
withTransientFields: <NewTransientFields extends Record<string, unknown>>(
defaultTransientFields: NewTransientFields,
) => DefineTypeFactoryInterface<Type, NewTransientFields>;
defaultTransientFields: Merge<TransientFields, NewTransientFields>,
) => DefineTypeFactoryInterface<Type, Merge<TransientFields, NewTransientFields>>;
}

export interface DefineTypeFactoryInterface<
Expand Down Expand Up @@ -144,31 +144,31 @@ export function defineTypeFactoryInternal<

class DefineTypeFactory extends Function {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
#defaultTransientFields: any;
_defaultTransientFields: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(defaultTransientFields: any) {
super();
this.#defaultTransientFields = defaultTransientFields;
this._defaultTransientFields = defaultTransientFields;

// ref: https://gist.github.com/arccoza/50fe61c8430fc97a463bf6b8960776ce
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
// @ts-expect-error Maybe this is a bug of TypeScript.
apply: (target, _thisArg, args) => target.#apply(...args),
apply: (target, _thisArg, args) => target._apply(...args),
});
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
#apply(options: any) {
return defineTypeFactoryInternal(Object.keys(this.#defaultTransientFields), {
_apply(options: any) {
return defineTypeFactoryInternal(Object.keys(this._defaultTransientFields), {
...options,
defaultFields: { ...this.#defaultTransientFields, ...options.defaultFields },
defaultFields: { ...this._defaultTransientFields, ...options.defaultFields },
});
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
withTransientFields(defaultTransientFields: any) {
return new DefineTypeFactory(defaultTransientFields);
withTransientFields(newDefaultTransientFields: any) {
return new DefineTypeFactory({ ...this._defaultTransientFields, ...newDefaultTransientFields });
}
}
export const defineTypeFactory = new DefineTypeFactory({}) as unknown as DefineTypeFactoryInterfaceRequired<{}, {}>;

0 comments on commit a14b6c0

Please sign in to comment.