i have this type TypeJsonRootKeys
ready to be consumed by other files, all working.
Sample json:
// sample.json
{
"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"
}
// file 1
import myjson from '../public/sample.json'
export type TypeJsonRootKeys = keyof typeof myjson
By external requirements i need to split this into two files. I don't know if this is possible, figuring out how to make the type TypeJsonRootKeys
get the values from another file.
// file1 Type declaration pending of type Parameters
export type TypeJsonRootKeys<AnyJson> = keyof typeof AnyJson ❓
Having a 2º file in another part of the project importing both the .json file and the type to assign that json and generate the keys.
// file2 where the json will be now accesible for import, and trying to assign to the type
import someJsonData from '../public/sample.json'
import type { TypeJsonRootKeys } from '@/file1'
// Assing here somehow
TypeJsonRootKeys<someJsonData> ❓
And have other files consuming TypeJsonRootKeys which should display
// file x
import type { TypeJsonRootKeys } from '@/file1'
// allowed values should be "greetings" | "commons" | "home"
const home:TypeJsonRootKeys = 'home' ✅
const house:TypeJsonRootKeys = 'house' ❌
i am with the doc, but cannot figure out how to achieve this. Thanks in advance!