Type that converts all strings to function that returns string
I have an object like I want to create a type that converts all the strings to functions that return strings. For example,
typeof bar.bar2.bar3 = () => "love you"
I have no idea how to do this lol17 Replies
I guess what you want to create is a function, not a type. So you would need a recursive function that does following things:
1. check if passed parameter is a primitive (string, or maybe number, bool, ...), if so return "() => parameter"
2. if param is array, map through it and call itself with each item as the new parameter
3. if param is object, map do the same for all objects props ( Object.keys(obj) )
I'm actually trying to lie to the compiler about the type
It's not actually a function that returns a string, but I want tsc (and autocomplete) to think it is
something like this im not sure, not a wizard
Oh alright. Then I'm out as well 😅
I would generally recommend using the Record<key, value> syntax instead of { [key]: value }. A bit more clear imo
This will cast bar as a string when it should be an object
I tried
Omit<Record<string, string>, keyof typeof myObject>
but it didn't work
The part I'm stuck on is only casting the strings, not the objects
maybe that will work
as const is important
sadly not
it should

global and home are objects
works for me

oh no kinda
wait it does work
vscode shows it as strings when you hover, but it doesn't complain when you invoke it

thanks barry you a g
dont thank me
thank some dude on typescript discord lol
InfectiousFight#0245
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View