Is there a way to define an interface in TypeScript where the keys are based on a specific type? For example:
type FruitTypes = "banana" | "apple" | "orange";
interface FruitInterface {
[key: string]: any; // should use FruitTypes as keys instead of string
}
// Desired output:
const FruitsObject: FruitInterface = {
banana: "Banana",
apple: "Apple",
orange: "Orange",
mango: "Mango" // This should cause an error
};
I attempted this approach:
interface FruitInterface {
[key: keyof FruitTypes]: any;
}
Is there another method to achieve this?
Thank you in advance.