Is it possible to create a new function type from an existing typescript function that has the same parameters and generics but without the return type?
Example:
I am trying to create the type ReturnNothing from the ReturnSame function without having to define it manually.
type ReturnSame = <T extends string>(a: T) => T
type ReturnNothing = <T extends string>(a: T) => void
Attempts:
function returnSame<T extends string>(a: T): T {
return a;
}
// Attempt 1: Does not work. Generics is not following to new type.
const returnNothing1 = (...params: Parameters<typeof returnSame>) => {
console.log(params);
};
// Attempt 2: Does not work. Return type is forced.
const returnNothing2: typeof returnSame = (...params) => {
console.log(params);
};
// Attempt 3: Does not work. OmitReturn is a made up Typescript utility.
const returnNothing3: OmitReturn<typeof returnSame> = (...params) => {
console.log(params);
};
// Usage
const same = returnSame<"a">("a");
const nothing1 = returnNothing1<"b">("b");