I am currently working on a declaration file where I need to establish a global type that is specifically tied to a predetermined list of string phrases. These string phrases are part of property keys within an object located in a JSON file.
I have a couple of questions:
- Can I import a JSON file into a declaration file and manipulate it using basic functions like
Object.keys
andArray.map
? - Additionally, can I utilize a JavaScript array of strings to define a type in TypeScript?
Let's delve into some code example for better clarification.
Imagine we have the following JSON file named data.json
:
{
"someList": {
"#key1": {"a": 1, "b": 2},
"#key2": "some value",
"#key3": 1234
}
}
My goal is to create the declaration file global.d.ts
as shown below:
import data from './data.json';
declare global {
type AllowedKeys = Object(data.someList).map(key => key.substr(1));
}
In essence, I need the type to be dynamically defined based on the contents of the JSON file, such as:
type AllowedKeys = "key1" | "key2" | "key3";
Any assistance or pointers on this matter would be greatly appreciated.