简体   繁体   English

将 object 传递给 function 时,如何指定 TypeScript 类型?

[英]How can I specify a TypeScript type when passing an object into a function?

I have a function:我有一个 function:

export default function AppNavbar({ isEditor }) {

I want to specify that isEditor is boolean .我想指定isEditorboolean

I tried:我试过了:

export default function AppNavbar({ isEditor: boolean }) {

but that doesn't seem to work.但这似乎不起作用。 Any assistance is greatly appreciated.非常感谢任何帮助。 Thanks!谢谢!

You need to specify the type after the parameter, eg您需要在参数后指定类型,例如

export default function AppNavbar({ isEditor }: {isEditor: boolean}) {...

You are on the right track, however the syntax you tried is already used in JavaScript for aliasing destructured variable names .您走在正确的轨道上,但是您尝试的语法已在 JavaScript 中用于别名解构变量名称

You can annotate destructured function parameters like this:您可以像这样注释解构的 function 参数:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Typescript 中,如何指定可以返回多种类型的函数的返回类型? - In Typescript, how can I specify the return type of a function that can return multiple types? 我如何在typescript中描述和调用object类型的function? - how can i describe and call a function of a type of object in typescript? 如何使用打字稿和节点从依赖项中指定类型? - How can I specify a type from a dependency with typescript and node? 传递给另一个函数时,如何在Typescript中设置函数的类型? - How to set the Type of a function in Typescript, when passing that into another function? 如何在typescript定义中指定松散类型的对象文字? - How can I specify loosely typed object literal in typescript definition? Typescript:当我只有一个类型名称时,如何构造一个特定类型的 object? - Typescript: How can I construct an object of a particular type when I only have a type name? 如何通过传递 object 作为参数来调用 javascript/typescript 构造函数? - How can I call a javascript/typescript constructor by passing an object as parameter? 在函数中使用动态导入时,如何在全局变量中指定类型信息? - When using dynamic import in a function, how can I specify type info in global variable? 在Typescript中,为什么对象不能在赋值时指定多余的属性,但是当它使用相同的接口传递给函数时呢? - In Typescript, why is it that an object cannot specify excess properties on assignment but it can when passed to a function, using same Interface? 我是否需要在 javascript/typescript 中为匿名函数指定返回类型? - Do I need to specify a return type for an anonymous function in javascript / typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM