DEV Community

Vladimir
Vladimir

Posted on

1 2

TypeScript. How to add a property to class method?

I have next code:


export const getPath = async (from: CurrencyTicker, to: CurrencyTicker): Promise<ExchangePair[]> => {
  const helper = new Helper(from, to);
  const path = await helper.calculatePath()

  return path;
}
getExchangePath.cached = withMemoryCache('exchange-path', CACHE_PAIR_COUNT, ms('5m'), getExchangePath);

It works fine, but how can I make same thing with Class?

Something like that

class Estimate {
  private readonly helper: Helper;

  constructor(currencyFrom: CurrencyTicker, currencyTo: CurrencyTicker) {
    this.helper = new Helper(currencyFrom, currencyTo);

    // next line throws TS error Property 'cached' does not exist on type '() => Promise<[string, string][]>'
    this.getExchangePath.cached = withMemoryCache('exchange-path', CACHE_PAIR_COUNT, ms('5m'), getExchangePath);
  }

  getPath = async (): Promise<ExchangePair[]> => {
    const path = await this.helper.calculatePath()

    return path;
  }
}

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Collapse
 
golubvladimir profile image
Vladimir Golub

Add interface

interface F { (): any; cached: number; }

class Estimate {
  ...

  getExchangePath = <F>function() {
    ...
  }
}
Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️