Reputation: 164
I have the following code
export interface A {
redApple: props[];
yellowApple: props[];
redYellowApple: props[];
}
export type FruitColour = "red" | "yellow | "green";
export type Fruit = "apple" | "banana | "cherry";
const x = [
"redApple",
"yellowApple",
"redYellowApple",
"greenCherry"
]
export const changeFruit = (
fruit: Fruit,
colour: Colour
): keyof A => {
return x
.filter((string) => string.match(colour + fruit))
.splice(0, 1)
.toString();
};
I keep getting Type 'string' is not assignable to type 'keyof IndexViewDataItem'. I can use myFunction(someField: keyof A | string) but then I lose type guarding.
Any help or guidance would be greatly appreciated.
Upvotes: 0
Views: 1031
Reputation: 164
Managed to get a work around for this by
export const changeFruit = (
fruit: Fruit,
colour: Colour
): keyof A => {
const y = x
.filter((string) => string.match(colour + fruit))
.splice(0, 1)
.toString();
return y as keyof A;
};
Not sure if this still enforces type guarding but I imagine so. If not I would still appreciate a better approach.
Upvotes: 1