library
is a module in Typescript that exports a specific function called someFunction
. This function is designed to be called with a string input that must be one of a defined set of values.
Within the library
, the someFunction
function is defined to only accept certain strings, such as "value1"
or "value2"
. However, the interface specifying these possible values is not accessible outside of the module.
Let's look at the user code below, where someFunction
is being used:
import { someFunction } from "library"
function doThing(text: string): void {
someFunction({
content: text,
})
}
When running this code, Typescript rightly throws an error stating that the type 'string'
is not compatible with the type
'"value1" | "value2" | ...'
.
The ideal solution would be to align the type of the text
argument in doThing
with the expected type in someFunction
. However, since the type is lengthy and not publicly accessible due to the unexported interface, importing it directly isn't possible.
So, the challenge remains: how can we correctly type the text
parameter without resorting to simply using any
?