2

I have a JavaScript class written in ES6:

// MyClass.js
export default class MyClass {
  myfunction() {
    console.log('myfunction called');
  }
}

I'd like to extend this class with a TypeScript class:

// MyTSClass.ts
import MyClass from './MyClass';

class MyTSClass extends MyClass {
  public initialize() {
    this.myfunction();
  }
}

This produces a TypeScript compiler error:

TS2339: Property 'myfunction' does not exist on type 'MyTSClass'

I have esModuleInterop set to true in my tsconfig.

I attempted to make a declaration file, thinking this might help:

// MyClass.d.ts
export = MyClass;

declare class MyClass {
  myfunction: void;
}

This makes the compiler happy, but in the generated code in the browser I see something unexpected, and the console log does not print:

class MyTSClass extends WEBPACK_IMPORTED_MODULE_1__["MyClass"]...

What am I doing wrong?

4
  • The compiler being happy and the generated code not functioning aren't necessarily related. You've done the right thing to get it to compile, now you need to figure out why your code isn't working. Start by reading the error you get, or if there is none, by figuring out exactly what it's doing, and what it needs to do instead. Commented Apr 27, 2019 at 19:24
  • In short, you've solved one problem, now you have another, and there's not nearly enough information here currently to qualify as a minimal reproducible example for reproducing the problem, because all you've said about it is "the functionality doesn't work". Commented Apr 27, 2019 at 19:26
  • Thanks, I updated the example to show a console.log in the original function that is not printed. I haven't included webpack config, tsconfig, etc. since I'm not sure which parts are relevant. I was hoping I was doing something wrong that was easily identifiable to one with experience, but can take the time to spin up a more complete example soon. Commented Apr 27, 2019 at 19:36
  • You're not instantiating the class and using it anywhere. Commented Apr 27, 2019 at 19:40

1 Answer 1

3

It's hard to tell from the info you've provided, but I would start at whatever is calling initialize. I'm assuming you didn't mean to use constructor instead? In Chrome, you can use the debugger; statement to launch the debugger at a specific point in your code, which I think is easier than setting breakpoints. From there, you can step through the code and see what's going on. You could also console.log the instance of your class to inspect its prototype chain to make sure the myfunction method is there.

Sign up to request clarification or add additional context in comments.

3 Comments

"Start debugging by looking around X" is not an answer. Best to wait for OP to clarify the question enough to make the question fully answerable.
I appreciate the explanation for the downvote, but I would argue debugging is how the OP will find the answer. Ultimately, if he learns to debug, he'll be better equipped to find answers on his own.
Thanks, I'm embarrassed to admit the problem was in the class that called initialize. I'm accepting the answer since it was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.