理解 NodeJs 打字稿函数体

我试图了解以下代码的作用


export const extractSomeDto = ({

    _id,

    name,

    status

}: SomeDto): SomeDto =>

transformAttributes<SomeDto>({

    _id,

    name,

    status

});

函数调用是这样的——


extractSomeDto(JSON.parse(body))

我可以弄清楚以下内容-

  1. 导出方法extractSomeDto供其他文件调用

  2. 该方法将返回: SomeDto

我有以下疑问 -

extractSomeDto 的输入是什么?
SomeDto还是_id, name, status

该方法extractSomeDto将调用另一个名为的方法transformAttributes
我可以看到_id, name, status作为参数传递,但我不明白这<SomeDto>部分


冉冉说
浏览 85回答 2
2回答

米脂

让我们分解一下:export&nbsp; &nbsp; export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>//&nbsp; ^^^^^^&nbsp; &nbsp; &nbsp; transformAttributes<SomeDto>({ _id, name, status });导出方法extractSomeDto供其他文件调用你是对的,它将从当前模块导出。小更正,这是一个功能,而不是一个方法。方法属于对象——你总是称它们为something.someMethod(). 函数没有与之关联的对象,因此它们本质上是“自由浮动”的,您可以将它们称为someFunction().输入参数export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&nbsp; &nbsp; transformAttributes<SomeDto>({ _id, name, status });此函数采用的参数是 类型SomeDto,但是属性_id、name和status将被解构。手册文档显示了一个变量声明,但它也可以用于参数。本质上,它只是从输入中获取这三个属性的值,并将它们分配给同名的变量。这比做input._id, input.name, 和更容易input.status。返回类型export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^&nbsp; &nbsp; transformAttributes<SomeDto>({ _id, name, status });该方法将返回: SomeDto你又是对的——返回类型SomeDto又是。(提醒它是一个函数,而不是一个方法)。因此,输入和输出是同一类型的对象。通用函数调用export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>&nbsp; &nbsp; transformAttributes<SomeDto>({ _id, name, status });//&nbsp; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^该函数将调用一个名为的通用函数transformAttributes。该函数的签名必须类似于通用类型参数function <T>transformAttributes()在哪里。T在您的例子中,类型参数是SomeDto. 函数的确切决定取决于实现,尽管它是其中之一参数类型,例如,function <T>transformAttributes(input: T)返回类型,例如,function <T>transformAttributes(): T输入和返回类型function <T>transformAttributes(input: T): T输入参数为transformAttributes()export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>&nbsp; &nbsp; transformAttributes<SomeDto>({ _id, name, status });//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^^^^^这是对象属性的简写语法。它相当于{ _id: _id, name: name, status: status&nbsp; }. 换句话说,它从这三个变量创建属性,其中每个名称是变量的名称,值是变量的值。transformAttributes和的返回值extractSomeDto最后,快速提一下——因为代码使用的是箭头函数,并且正文没有包含在大括号中,{}所以隐式地返回值就是正文返回的任何值。这意味着返回调用时extractSomeDto的结果。transformAttributes<SomeDto>({ _id, name, status })

暮色呼如

函数名称:extractSomeDto功能参数:{_id,name,status}: SomeDtoan。这意味着该函数接受一个 SomeDtoan 类型的对象并将其解构以提取以下字段:_id、名称、状态。函数返回类型:SomeDto函数体:函数返回结果transformAttributes<SomeDto>({&nbsp; &nbsp; _id,&nbsp; &nbsp; name,&nbsp; &nbsp; status});这是相同的功能,重写后更容易理解。export const extractSomeDto = (inputObject: SomeDto): SomeDto => {&nbsp; &nbsp;const _id = inputObject._id;&nbsp; &nbsp;const name = inputObject.name;&nbsp; &nbsp;const status = inputObject.status;&nbsp; &nbsp;return transformAttributes<SomeDto>({_id, name, status});}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript