Let's break it down:
export
export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
// ^^^^^^
transformAttributes<SomeDto>({ _id, name, status });
- the method
extractSomeDto
is exported for other files to call it
You are correct it will be export from the current module.
Minor correction, this is a function, not a method. Methods belong on objects - you always call them as something.someMethod()
. Functions don't have an object they are associated with, so they are essentially "free floating" and you can call them as someFunction()
.
Input parameter
export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
transformAttributes<SomeDto>({ _id, name, status });
The parameter this function takes is of type SomeDto
, however, the properties _id
, name
, and status
are going to be destructured. The handbook documentation shows a variable declaration but it can also be used for parameters.
In essence, it just takes the values of those three properties from the input and assigns them to a variable with the same name. It's easier than doing input._id
, input.name
, and input.status
.
Return type
export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
// ^^^^^^^
transformAttributes<SomeDto>({ _id, name, status });
- the method is going to return
: SomeDto
You are again correct - the return type is SomeDto
again. (reminder it's a function, not a method).
So, the input and output are the same type of object.
Generic function call
export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
transformAttributes<SomeDto>({ _id, name, status });
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The function will call a generic function called transformAttributes
. The signature of that function must be something like function <T>transformAttributes()
where T
would be the generic type argument. In your case, the type argument is SomeDto
. What exactly that determines for the function depends on the implementation although it's one of
- The parameter(s) type, e.g.,
function <T>transformAttributes(input: T)
- The return type, e.g.,
function <T>transformAttributes(): T
- Both the input and the return type
function <T>transformAttributes(input: T): T
Input argument for transformAttributes()
export const extractSomeDto = ({ _id, name, status }: SomeDto): SomeDto =>
transformAttributes<SomeDto>({ _id, name, status });
// ^^^^^^^^^^^^^^^^^^^^^
This is object property shorthand syntax. It's equivalent to { _id: _id, name: name, status: status }
. In other words, it creates properties from these three variables where for each the name is the name of the variable and the value is the value of the variable.
Return value of transformAttributes
and extractSomeDto
Finally, a quick mention - since the code is using an arrow function and is body is not wrapped in curly brackets {}
then implicitly the return value is whatever the body returns. This means that extractSomeDto
returns the result of transformAttributes<SomeDto>({ _id, name, status })
when called.