#Errors when constructing class based on variables

1 messages · Page 1 of 1 (latest)

potent stratus
#

It's a bit hard to describe with words. The problem is that I want to dynamically create/initialize classes. Different classes have different constructor parameters... But somehow I'm not able to get the typing correct.

main saffronBOT
#
thomasjonas#4425

Preview:ts ... const id = "performance-module" const m = modules[id] const copy = {text: "string"} const preperation = m.prepare() // can not get this to work: new m({ copy, ...preperation, }) ...

hasty spoke
#

the problem is the way you're setting up modules using dynamic property names (like [VenueModule.ID]: VenueModule)

#

that treats VenueModule.ID as an arbitrary string rather than the exact literal type, and modules ends up becoming a Record<string, typeof PerformanceModule | typeof VenueModule>

#

if you instead write out the property names, things work out the way you want:

main saffronBOT
#
mkantor#7432

Preview:```ts
type Copy = {
text: string
}

type PerformanceModuleConstructor = {
copy: Copy
} & ReturnType<typeof PerformanceModule.prepare>

type VenueModuleConstructor = {
copy: Copy
} & ReturnType<typeof VenueModule.prepare>

class PerformanceModule {
static ID
...```

potent stratus
#

Wow, amazing. That actually fixes it. Thank you so much!

#

!resolved

tacit shadow
#

Hi @potent stratus and mkantor, I’m Alexander Lill (a real person), a researcher at the University of Zurich. I’m trying to help people receive answers faster on Discord. If you are not ok with giving me some feedback, click the 🚪 symbol, and I will delete this message and apologize for the inconvenience.

#

Would any of the provided links have helped to answer the OPs question? Please click the number-emoji for each link that would have been helpful, and on the ❌ if none of the links would have been helpful to answer the OPs question.

1️⃣ Typescript initialize static variable of a class type
https://stackoverflow.com/questions/24330620

2️⃣ How to use TypeScript constructor to declare variables
https://stackoverflow.com/questions/37550805

3️⃣ Problem when trying to instantiate a class dynamically
https://stackoverflow.com/questions/62735483

Let me know if you have any questions! More info: #1063200184315674706

main saffronBOT
#

:warning: Please wait a bit longer. You can ping helpers <t:1675958511:R>.

potent stratus
#

@hasty spoke hmm, maybe a bit too quick. What if the key that selected the right class is also a variable?

main saffronBOT
#
thomasjonas#4425

Preview:ts ... const options = [ "venue-module", "performance-module", ] as Modules[] const id = options[Math.floor(Math.random() * options.length)] const m = modules[id] const copy = {text: "string"} const preperation = m.prepare() // can not get this to work: new m({ copy, ...preperation, }) } ...

hasty spoke
#

i think you'll have to narrow down m to a specific constructor so that typescript can check that the object you pass in is usable. typescript doesn't know how to correlate preparation with the specific m that you use later on

#

here's an example of how you could handle this via narrowing:

main saffronBOT
#
mkantor#7432

Preview:```ts
type Copy = {
text: string
}

type PerformanceModuleConstructor = {
copy: Copy
} & ReturnType<typeof PerformanceModule.prepare>

type VenueModuleConstructor = {
copy: Copy
} & ReturnType<typeof VenueModule.prepare>

class PerformanceModule {
static ID
...```

hasty spoke
#

i'm not sure if/how you're using modules elsewhere in your code, or what the motivation behind these prepare() methods is, but you might be able to simplify things further

potent stratus
#

I was trying to keep everything as dynamic as possible, but I understand TypeScript does not automagically knows what I mean. I will try to make it a bit simpler. I'm probably overcomplicating things. Thanks again!

tacit shadow
#

Thank you for your feedback @potent stratus, and sorry that the links were not helpful!

I’d like to invite you and everyone in this conversation to fill in a short follow-up survey on your experience with the suggested links and your feedback (max. 10 mins).

We would highly appreciate your feedback to help our research project. For your efforts, you can decide to be entered in a raffle for an Amazon gift card of USD 100 value (voluntary).

To participate in the survey, please answer the questions in the following link. Your responses will be treated confidentially.
https://uzhwwf.qualtrics.com/jfe/form/SV_1zWdrjjRfwFGXwa?ST=SO&CO=TS&CI=1858153&LA=1