I'm typescript beginner, i have set types for props as you can see but getting error: Type 'undefined' cannot be used as an index type.ts(2538) (property) AA.action?: string | undefined
any advice/help ? my code:
interface AA {
action?: string;
actionArgs?: string;
}
const ArticleList: React.FC<AA> = (props) => {
const data = useSelector((state: RootStateOrAny) => {
const secondData = {
sites: state.articles.sites,
analysers: state.siteArticles.analysers,
};
return secondData[props.action];
});
if i remove optional then getting more errors: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ sites: any; analysers: any; }'. No index signature with a parameter of type 'string' was found on type '{ sites: any; analysers: any; }'.ts(7053)
interface AA {
action: keyof MySecondData;
actionArgs?: string;
}
interface MySecondData {
sites: string_I_guess[],
analysers: Analysers[],
};
const ArticleList: React.FC<AA> = (props) => {
const data = useSelector((state: RootStateOrAny) => {
const secondData: MySecondData = {
sites: state.articles.sites,
analysers: state.siteArticles.analysers,
};
return secondData[props.action];
});