这种参数'const types = ({ editor }: { editor: Editor }) => {}'是什么意思?



>我最近在看 https://github.com/ory/editor/blob/master/packages/ui/src/Trash/index.js#L89

并找到了一种我不明白的论点。 以下是完整的代码:

const types = ({ editor }: { editor: Editor }) => {
const plugins = [
...Object.keys(editor.plugins.plugins.layout),
...Object.keys(editor.plugins.plugins.content)
].map(
(p: string) =>
editor.plugins.plugins.content[p].name ||
editor.plugins.plugins.layout[p].name
)
if (editor.plugins.hasNativePlugin()) {
plugins.push(editor.plugins.getNativePlugin()().name)
}
return plugins
}

这个论点是什么意思?叫什么?

这意味着函数将接收一个带有编辑器属性的对象,并且具有编辑器的类型。

有关更多信息,您可以查看 https://flow.org/en/

所以这里有两个部分。

  1. 您破坏给定的参数并仅使用editor属性
    { editor }
  2. 定义传递对象的类型。

如果没有类型定义,它看起来像这样。如果你知道,你只需要一个传递对象的编辑器,你可以破坏它

// Passing and working with the whole object
const fn1 = ( obj ) => {
const editor = obj.editor;
console.log( editor );
};
// Destructing the object and only use the editor property
// Basically the same as fn1 without the whole obj.
const fn2 = ( { editor } ) => {
console.log( editor );
};
const obj = {
editor: 'Editor',
};
fn1( obj );
fn2( obj );

相关内容

最新更新