I am using a library called apprun. The Component class type defintion (here is its implementation) looks as follow:
export type View<T> = (state: T) => string | VNode | VNode[] | void;
export class Component<T=any> {
constructor(state?: T, view?: View<T>, update?: Update<T>);
readonly state: T;
setState(state: T, options?: { render?: boolean, history?: boolean }): void;
mount(element?: Element, options?: { render?: boolean, history?, global_event?: boolean }): Component<T>;
start(element?: Element, options?: { render?: boolean, history?, global_event?: boolean }): Component<T>;
on(name: string, fn: (...args: any[]) => void, options?: any): void;
run(name: string, ...args: any[]): number;
rendered: (state: T) => void;
mounted: (props: any) => void;
unmount: () => void;
unload: () => void;
}
When I use this class in my own components, I am having issues with the type inference:
interface State {
foo: string
}
export class AboutPage extends Component<State> {
// State is correctly inferred.
// Changing `foo` to be `1` will cause an error.
state = {
foo: 'bar'
}
// Parameter `state` implicitly has an 'any' type.
// Why is the parameter not inferred just like the `state` property?
view = (state) => <div>About</div>
}
What I am having trouble with is understanding why the type for the property state
is inferred and why isn't the same thing happening for the parameter state
?