I am new to js/ts but experienced in C#. Now I'm learning React and want to use typescript. I try to add types to this demo project: https://github.com/bradtraversy/projectmanager/tree/master/src .
Code so far:
export interface IProjectItem {
title: string;
category: string;
}
export interface IProjects {
projects : {
[index: number]: IProjectItem;
};
}
class App extends React.Component<object,IProjects> {
constructor(props: IProjects) {
super(props);
this.state = {
projects: [
{
title: 'Business Website',
category: 'Web Design'
},
....
}
render() {
return (
<div className="App">
MyApp
<Projects projects={ this.state.projects }/>
</div>
);
}
}
class Projects extends React.Component<IProjects,object> {
render() {
if (this.props.projects)
{
this.props.projects.map( project => { //ERROR Property map not found for type [index:number]
console.log(project);
});
}
console.log(this.props);
return (
<div className="projects">
All Projects
</div>
);
}
}
Obviously the .map() function is not found. I think I have to change the used interfaces/types, but what is the right one?