So I have a React project bootstrapped with create-react-app. I'm converting it to Typescript gradually and have, for example, something like:
App.tsx
import React from 'react';
import { TestThing } from './components/TestThing'
function stuff(a: any): void {
console.log('stuff', a);
}
function App() {
return (
<div className="App">
<TestThing foo="foo" bar={stuff}/>
</div>
);
}
export default App;
and a legacy file TestThing.js
import React from 'react';
const DEFAULT_THING = () => {
console.log('whatever');
};
export const TestThing = ({ foo, bar = DEFAULT_THING }) =>
<div onClick={bar}>foo = {foo}</div>;
This gives the error:
Type '(a: any) => void' is not assignable to type '() => void'. TS2322
in App.tsx as I presume Typescript is inferring bar can take no arguments because of it's default assignment in the legacy JS code.
How can I tell TS this is valid without having to touch that legacy JS file?
Thanks!
DEFAULT_THINGshould have the same signature asstuff. Either removea: anyfromstuffor add it toDEFAULT_THINGlist of arguments.