Is it safe to extract functions from `browser` API? Do they internally use `this`?

Can I safely do this:

export const tr = browser.i18n.getMessage;

To have a short function name for translating?
I know this will break the “this” inside the getMessage, but if it’s not using it, it will work just fine.

So my questions:
Is this a good idea in the first place?
Is this an implementation detail that I can’t rely on?
Would it also work on other browser API, like storage / tabs, etc…? (I will probably not use it, just curious).

EDIT:
Maybe there is a better way using Proxy API?

Why not just define a wrapper function instead?

export const tr = (...args) => browser.i18n.getMessage(...args);

Theoretically, of course you cannot rely on the private implementation of any function. Practically, on the one hand the webextensions dev team appears to be very small so it’s unlikely to change any time soon, but there’s ongoing work within Firefox to switch i18n to Fluent so perhaps this will force an implementation change at some point in the next few years.

1 Like

Thank you for the info!
You are right, the wrapper will always be the best option.

The reason why I was looking for this “nasty shortcuts” was that I’ve started to refactoring my whole codebase into TypeScript and I had issues finding a proper type for the ...args parameter.
I don’t know what was I thinking… sometimes I just get this “out of box” crazy ideas.

But anyway, after two weeks of fulltime TypeScript refactoring this kind of things are a piece of cake :smiley: :

const tr = (msg: string, substitutions?: string | string[]) => browser.i18n.getMessage(msg, substitutions);