Why does the following code not cause a compile-time error?
import * as React from 'react';
export class Test extends React.Component {
private _onReferenceUpdated = (ref: HTMLCanvasElement) => {
ref.width = 4; // This could throw, when ref is null
};
render(): JSX.Element {
return (
<canvas ref={this._onReferenceUpdated} />
);
}
}
The ref attribute is inferred as
(JSX attribute) React.ClassAttributes<HTMLCanvasElement>.ref?: string | ((instance: HTMLCanvasElement | null) => void) | React.RefObject<HTMLCanvasElement> | null | undefined
which seems correct (the string is a bit weird, but i guess that's just generally for attributes). How is (ref: HTMLCanvasElement) => void assignable to (instance: HTMLCanvasElement | null) => void?