I encountered an issue with the provided example.
I'm uncertain about how to resolve it.
Your assistance would be greatly appreciated.
type TestValue = {
value: string;
};
type FirstTest = {
type: 'text';
text: TestValue[];
};
type SecondTest = {
type: 'heading';
heading: TestValue;
};
type Test = FirstTest | SecondTest;
const firstTest: Test = {
type: 'text',
text: [
{
value: 'Text!!',
},
],
};
const secondTest: Test = {
type: 'heading',
heading: {
value: 'heading!!',
},
};
const foo = (data: Test) => {
const { type } = data;
const content = data[type]; //Encountering error TS7053: Element implicitly has an 'any' type because expression of type '"text" | "heading"' can't be used to index type 'Test'. Property 'text' does not exist on type 'Test'.
switch (type) {
case 'heading':
return content.value;
case 'text':
return content[0].value;
}
};
Unfortunately, I am faced with the error
TS7053: Element implicitly has an 'any' type because expression of type '"text" | "heading"' can't be used to index type 'Test'. Property 'text' does not exist on type 'Test
Executing the foo function results in a type validation issue.