#How to dynamically change input type in a class method

1 messages · Page 1 of 1 (latest)

spiral sparrow
#

since Mock is Record<string, any>, keyof typeof BaseFactory.Mock will always be string. nothing in those types is related to T (and they can't be, since they are static members)

#

why are those two fields static? is there a way they can be instance properties instead?

knotty mortar
#

this snippet is smaller version of my project, where i am creating classes to perform crud operations on the dynamoDB table.
i have many models, like User, Comment, Blog etc, i dont want to write same logic in each factory class of every model, thats why i am creating a BaseFactory first, defining all the methods here, to create specific factory all i will have to do is

class UserFactory extends BaseFactory<typeof userMock, "username"> {
  static hashKey = "username";
  static Model = UserModel;
  static mock = userMock;
}```
#

is there any fix for line 31 ?

spiral sparrow
#

you can think of type parameters on classes as like type parameters for the constructor. they are applied to instances of the class, not static stuff

#

trying to somewhat-directly translate what you attempted, here's one idea:

idle geodeBOT
#
mkantor#0

Preview:```ts
const makeBaseFactory = <M, K extends keyof M>() =>
class BaseFactory {
static Mock: M
static hashKey: K
_body: Partial<M> = {}

static body(obj: Partial<M>) {
  const inst = new t

...```

spiral sparrow
#

that might look a big weird depending on how familiar you are with how classes work in JS/TS, so let me know if you have any questions

#

personally though i'd probably try to avoid all these factories and just use normal constructors

#

(i'm not a fan of complex OOP stuff in general, i find it makes the actual logic hard to follow and results in overly-tight coupling)

#

in your real code what does update actually do?

knotty mortar
idle geodeBOT
#

@knotty mortar Here's a shortened URL of your playground link! You can remove the full link from your message.

arc_xo_63472#0

Preview:```ts
import "server-only"

import {ModelType} from "dynamoose/dist/General"
import {AnyItem} from "dynamoose/dist/Item"
import {BaseSerializer} from "../serializers/base.serializer"
import {MentorMock} from "../models/mentor.model"
import {MentorModel} from ".."
...```

spiral sparrow
#

i suspect these may not really be "factories" in the typical usage of that term, really just base classes. do you eventually have class User extends UserFactory in your code?

#

or maybe there is no such thing as a User/Mentor class, just those factories?

idle geodeBOT
#
mkantor#0

Preview:ts const makeModel = <M, K extends keyof M>() => ({ body(obj: Partial<M>) { return { update(pk: M[K]) { // dynamic input type based on K console.log("primary Key: ", pk) return Math.random() > 0.5 ? false : true }, } }, ...

knotty mortar
#

whoa, i knew maps {} were objects in js, but didn't think of using it like this, thanks mate 👍

#

it is more concise and serves the purpose