0

I am trying to export a function inside of a class in Typescript. I am able to export the class, and use it as an import inside of another class. However, when I try to use the function, it gives me the error as follows

Property 'formatBytes' does not exist on type 'typeof Landing'.

I am attempting to export the function formatBytes inside the Landing class and use it as Landing.formatBytes inside the Modules class.

Exported class

import * as React from 'react';

export default class Landing extends React.Component<{}, SomeState> {
public formatBytes(bytes: number, decimals: number): string {

return 'something';
}

public componentDidMount(): void {

// code
}


public render(): JSX.Element {
const { items } = this.state;

return (
  <div>

  </div>
);
}
}

Imported class

import * as React from 'react';
import Landing from './Landing'

export default class Modules extends React.Component<
{},
IDetailsListModulesState
> {

constructor(props: {}) {
super(props);

const _columns: IColumn[] = [
  {
    onRender: (item: IDetailsListModuleItem) => {
      return (
        <span>
          {Landing.formatBytes(item.sizeDifference, 3)}
        </span>
      );
    }
  },
];

this.state = {
};
}

public componentDidMount(): void {

}

public render(): JSX.Element {

}
}

1 Answer 1

6

The Landing class must be instantiated in order for you to use the formatBytes method. Either instantiate a new instance of Landing by doing:

const myLanding = new Landing();
myLanding.formatBytes(item.sizeDifference, 3);

Or make formatBytes static by writing public static formatBytes...

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

Comments

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.