There is an intriguing scenario where dynamically defining types from a centralized JSON data store would prove extremely beneficial. Allow me to elaborate.
The JSON file I possess contains a roster of various brands.
// brands.json
{
"Airbus": {
"keywords": ["A320", "A380"],
"type": ["planes"]
},
"Jaguar": {
"keywords": ["fiesta"],
"origin": "UK",
"type": ["cars", "glasses"]
},
"Nissan": {
"keywords": ["qashqai"],
"type": ["cars"]
}
}
Next, there are type definitions in place:
import brands from "./brands.json";
type BrandNames = keyof typeof brands;
type Brands = {
[P in BrandNames]: {
keywords: string[];
origin?: string;
type: string[];
}
};
I've established a type called CompanyNames, which is automatically generated and encompasses "Airbus" | "Jaguar" | "Nissan"
.
Progressing smoothly so far...
Now, the objective is to form a type named CarBrands, representing "Jaguar" | "Nissan"
.
While
type CarBrands = Exclude<CompanyNames, "Airbus">;
could achieve this, it lacks the desired dynamism.
Hence, the aim is to filter out all keys from Brands that possess a nested type
property not containing the string "cars"
.
Is such a maneuver achievable?