Is it possible to replace the function signature of an external package with custom types?
Imagine using an external package called translationpackage
and wanting to utilize its translate
function.
The original function signature from the package is:
// original signature
function translate(key: string): string;
Now, I want to modify it to only accept keys 'foo' | 'bar'
. This would look like the following implementation if I were to redefine the translate
function myself.
// my own signature
function translate(key: 'foo' | 'bar'): string
What steps do I need to take in order for Typescript to use my custom signature instead of the original one whenever I import translate
?
import { translate } from 'translationpackage'
Edit: I am aware that d.ts
files can be used when the imported module lacks its own type definitions. However, it seems this does not apply when attempting to override existing types.
The code snippet below is close to what I'm aiming for, but Typescript disregards it in favor of the predefined types from the package.
declare module 'translationpackage' {
export function translate(key: 'foo' | 'bar'): string;
}