For some React Components we may meet some error about props type, sometimes we should check the type of function and non-function props.
Simple Example:
ts
interface Props<VT> {
value?: VT;
onChange?: (value: VT) => void;
}
function test<VT>(props: Props<VT>) {}
test({
1, // Type 'number' is not assignable to type 'string'.
value: onChange(v: string) {
return v;
},
});
Complex Example:
ts
interface baseProps<VT> {
value?: VT;
}
interface Props<VT> extends baseProps<VT> {
onChange?: (value: VT) => void;
}
class Component<P> {
constructor(props: P) {}
}
class MyComp<VT> extends Component<Props<VT>> {}
const comp = new MyComp({
1, // Type 'number' is not assignable to type 'string'.
value: onChange(v: string) {
return v;
},
});