Last active
September 10, 2020 22:09
-
-
Save willhoney7/11f9596068cd22ccf95f375b0cdd4a0e to your computer and use it in GitHub Desktop.
For function return types: Use "unknown", not "void" or "any"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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