The functionality of this code snippet is rather straightforward; it either returns a function or a string based on an inner function parameter.
function strBuilder(str: string) {
return function next(str2?: string) {
if(typeof str2 === "string") {
return strBuilder(str + str2)
} else {
return str
}
}
}
Unfortunately, there is a type error that occurs when executing the following expression:
strBuilder("Hello, ")("World")()
I am seeking assistance in properly annotating this function to eliminate the type error, as inference is not providing the desired results.
I have conceptualized a type for this function, but I am unsure how to specify that it should conform to that particular type.
type StringBuilder<S extends string> = (
str: S
) => (str2?: S) => S extends string ? ReturnType<StringBuilder<S>> : string
Any guidance or support on this matter would be greatly appreciated.
You can find a demo of the issue in the provided playground link here.