In TypeScript, I am aiming to create an overloaded function with named parameters. Despite the code running correctly, I encounter warnings about `init.index` potentially not existing in one of the function signatures.
The purpose of overloading is to offer mutually exclusive properties on the object based on the specifications in the documentation, which are not explicitly defined in the official library.
export function insertText(init: { text: string, index: number }): docs_v1.Schema$Request;
export function insertText(init: { text: string, segmentId?: string }): docs_v1.Schema$Request {
return {
insertText: {
text: init.text,
...(init.index && { location: { index: init.index} }),
...(!init.index && { endOfSegmentLocation: { segmentId: init.segmentId } }),
}
}
}
I have included a TypeScript playground which showcases examples of the code and the associated errors.
Furthermore, I aim to destructure the object parameter, as detailed here.