#How to fix "this expression is not constructable"?

39 messages ยท Page 1 of 1 (latest)

pulsar lintel
#

Hi everyone, I'm running into an issue I've never seen before. I'm creating a class that implements a particular interface. Now, I want to pass that class to a function and create a new instance of that class from that function. However, I run into the error message from the title, even though I can create new instances outside of the function. What am I missing here? Here's a link to an example code:

edit: see below

Reading online a bit about this is looks like it has something to do with how I'm importing the class. But as you can see i this code, it's all in the same file. I have no idea what's happening ๐Ÿคทโ€โ™‚๏ธ Thanks!

south abyssBOT
pulsar lintel
vital pivot
#

CutomTimer and GameTimer types refer to an object/instance

#

If you wanted a GameTimer class you could use typeof GameTimer, the type of GameTimer class is typeof GameTimer of course

#

If you make CustomTimer into a class too, you can do the same thing. Otherwise a custom constructor type (new () => CustomTimer) is fine too.

pulsar lintel
vital pivot
#

You can use typeof on a class, which is a value

#

But you can't use typeof on an interface, as that is a type

pulsar lintel
#

Oh yeah, my bad. I did use it on the interface.

#

That simplifies things quite a bit then, I've been panicking over nothing ๐Ÿ˜„

vital pivot
#

typeof AClass is basically { new (): AClass ...static props... }

pulsar lintel
#

However, if I wanted to make this function more generic I would have to use a constructor definition anyway.

vital pivot
#

Which is how you write a function with properties

#

Possibly

#

I know I've done that before too

pulsar lintel
#

Not in this example but maybe in the future, for similar objects implementing the same interface, it could be a problem.

#

Well, I'll go with this for now. Sometimes it's best to take a break and come back to it fresh. Thank you very much for your help!

vital pivot
#

Problem? Why?

#

Yeah I don't think writing a new signature is bad

pulsar lintel
#

Well, just thinking. If I need to create an additional class using the same interface now I have to extend also the signature of the function to accept either one of them.

vital pivot
#

A JS class is basically just a constructable function with optional properties

pulsar lintel
#

It's not a problem per se, just something to consider.

vital pivot
#

If you use a new => CustomTimer yeah it is flexible

#

But can you make CustomTimer a class?

#

I think that would be my first choice

#

You can also reduce duplication in derived timers, by putting common features there

pulsar lintel
#

In this case yes, it would also make more sense. But I'm trying to make it as generic as possible (also to help me understand TypeScript better).

vital pivot
#

Ok

pulsar lintel
#

Definitely, but using another example of a factory method where I need to instantiate multiple similar classes using the same interface this can be useufl to know how to implement in TypeScript. I haven't tried so I don't know how easy or difficult it really is though, I'm just trying to think ahead of this.

vital pivot
#

Yeah no problem with that

#

I would prefer a base class. But we don't have easy multiple inheritance, so if you want to inherit from X but also satisfy interface Y, then you'd do this.

#

Unless you want to go down the mixins route

pulsar lintel
#

Not really ๐Ÿ˜„ I'd rather keep things as simple as possible

#

But I keep over-complicating them when it comes to TypeScript. It seems to be either straight forward, or massively complicated.

vital pivot
#

๐Ÿคทโ€โ™‚๏ธ

#

TS is complicated sometimes, but I guess it takes experience

#

Sometimes it's just a question of learning the patterns that are TS friendly

pulsar lintel
#

Indeed it is, I'm trying to learn when to step aside and let it do its thing rather than being too clever about things. But in any case, thanks again for the help I'll take a little break for now ๐Ÿ™