The example below outputs an instance of SomeClass - this works as expected. Question: Is there a way to omit the DisplayModelComponentProps interface and directly pass the object to be displayed as prop?
class SomeClass {
public someProperty:string = "some string";
public someFunction():string { return "another string"; }
}
interface DisplayModelComponentProps {
obj:SomeClass
}
const DisplayModelComponent: React.FC<DisplayModelComponentProps> = props => {
return(<p>{props.obj.someProperty}</p>
)
}
export function Test(): JSX.Element {
const someObj:SomeClass = new SomeClass();
const props = { obj: someObj }
return(<div><DisplayModelComponent {...props}></DisplayModelComponent></div>)
}
When I change the code so the DisplayModelComponent takes a SomeClass as prop I get this error (because the function is lost on destructuring):
Property 'someFunction' is missing in type '{ someProperty: string; }' but required in type 'SomeClass'.