// utility type for declaration.
// Go through a generic object and replace all instances of a type with another type.
type ReplaceType<Type, FromType, ToType> = Type extends FromType // FromType?
? ToType // Yes, replace it
: Type extends object // If the key in the object is another object, call the function again; else use the basic type it has declared. ToType would've replaced it already if it was our source type
? ReplaceTypes<Type, FromType, ToType> // Yes
: Type; // No, leave it alone
export type ReplaceTypes<ObjType extends object, FromType, ToType> = {
[KeyType in keyof ObjType]: ReplaceType<ObjType[KeyType], FromType, ToType>; // Call above function for every key in the object
};
/* example
type k = {
a: number;
b: string;
};
type l = ReplaceTypes<k, string, boolean>;
const _x: l = {
a: 1,
b: true
};
*/
const fkt = () => {
return 'awd';
};
type l = ReplaceTypes<typeof fkt, string, boolean>;
const _x: l = () => {
return '';
};
let _o = _x();
Above script works great for objects, but has problems with functions as _x() complains of a missing call signature which shouldn't have been replaced in the first place. How can i match for "callable" and then not modify the type so the compiler knows whats up or modify the type so the call signature is added?