Skip to content

Instantly share code, notes, and snippets.

@willhoney7
Last active September 10, 2020 22:09
Show Gist options
  • Save willhoney7/11f9596068cd22ccf95f375b0cdd4a0e to your computer and use it in GitHub Desktop.
Save willhoney7/11f9596068cd22ccf95f375b0cdd4a0e to your computer and use it in GitHub Desktop.
For function return types: Use "unknown", not "void" or "any"
// when passing functions as props and you don't care about the return value, use "unknown"
type Props = {
onUpdate: () => unknown; // Use "unknown"!
};
function Component({onUpdate}: Props) {
return <button onClick={onUpdate}>update</button>
}
// With "unknown", both of these are valid!
function updateNoReturn() {
doSomething(); // notice, no return
}
function updateWithReturn() {
return doSomething();
}
<Component onUpdate={updateNoReturn} /> // OK!
<Component onUpdate={updateWithReturn} /> // OK!
// Why not "void"?
type Props = {
onUpdate: () => void;
}
<Component onUpdate={updateNoReturn} /> // OK!
<Component onUpdate={updateWithReturn} /> // ERROR!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment