Currently, I am in the process of implementing some functions in Typescript that I plan to overload, and they involve the usage of generics. However, I am encountering a confusing issue that has left me puzzled:
*Please disregard the irrelevant code snippets that have been excluded, as they do not contribute to the problem at hand.
The following code snippet works:
export function persistContactData<T>(
contactData: T,
callback?: () => void
): void;
export function persistContactData<T>(
{ contactData }: { contactData: T },
callback?: () => void
): void;
export function persistContactData<T>(
{ contactData }: { contactData: T },
callback?: () => void
) {
// implementation
}
However, the following snippet encounters an error on the first function:
// *** Compile error: "This overload signature is not compatible with its implementation"
//
export function getPersistedContactData<T>(
contactId: string,
callback: (result?: T) => void
): void;
// There are no more errors when I remove the above signature.
export function getPersistedContactData<T>(
{ contactId }: { contactId: string },
callback: (result?: T) => void
): void;
export function getPersistedContactData<T>(
{ contactId }: { contactId: string },
callback: (result?: T) => void
) {
// implementation
}
I am struggling to pinpoint the exact issue here, but I suspect it has something to do with how the generic parameter is utilized. However, in theory, this should not be causing any problems.
(The Typescript version I am using is 3.9.3)
Edit: I made updates to remove any unknown types