I'm importing a .json file, and i want to get all the keys as optional parameters
import jsonFile from '../public/sample.json'
The json has unknown keys but, for the question, let's say it has the following:
{
"greetings": {
"hello": "Hello",
"hello_informal": "Hi",
"good_morning": "Good morning",
"good_afternoon": "Good afternoon",
"good_night": "Good night",
"bye": "Bye"
},
"commons": {
"pagination": {
"first_page": "First page",
"last_page": "Last page",
"next_page": "Next page",
"previous_page": "Previous page"
}
},
"home": "Home sweet home"
}
And i have a function with 1 parameter.
function sample(keyValue: TypeKeyValue) {
// logic here...
console.log("Thank you Stackoverflow", keyValue)
}
My question is how create a type for that keyValue
param for all the object keys (nested keys included, prefixed by parent keys with a dot).
Value of the keys should not matter, just collect all the keys as optional.
i was trying this but is not working as expected:
type TypeKeyValue: keyof jsonFile
The desired result should be an automated way to get the following:
type TypeKeyValue: "greetings" | "greetings.hello" | "greetings.hello_informal" | "greetings.good_morning" | "greetings.good_afternoon" | "greetings.good_night" | "greetings | bye" | "commons" | "commons.pagination" | "commons.pagination.first_page" | "commons.pagination.last_page" | "commons.pagination.next_page" | "commons.pagination.previous_page" | "home"
Any help or solution would be much appreciated