I have a function:
export default function AppNavbar({ isEditor }) {
I want to specify that isEditor is boolean.
I tried:
export default function AppNavbar({ isEditor: boolean }) {
but that doesn't seem to work. Any assistance is greatly appreciated. Thanks!
You are on the right track, however the syntax you tried is already used in JavaScript for aliasing destructured variable names.
You can annotate destructured function parameters like this:
type ItemType = {
isEditor: boolean;
};
// The type needs to be specified after the parameter:
function doSomething({isEditor}: ItemType) {
const alias: boolean = isEditor;
}
// Inline type annotation works too:
function doSomethingElse({isEditor}: {isEditor: boolean}) {
const alias: boolean = isEditor;
}
// You can't type the destructured fields because that syntax is already used
// for aliasing the destructured parameters. Here, 'isEditor' is aliased to
// 'someOtherVariableName'.
function doSomethingWithAliasedArg({isEditor: someOtherVariableName}: ItemType) {
// Notice 'someOtherVariableName' is used instead of 'isEditor'
const alias: boolean = someOtherVariableName;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With