0

I had not come across such a notation:

function(a: number,
sort?: { order: -1 | 1; sortBy: string }[] ){
//function body here
}

Specifially I'm interested in the sort parameter.

I can only understand that sort is an optional argument (I can't even say for sure if it's an object literal since there's no ,).
What does this syntax read? What type of variable is it? Why is there a ; instead of a ,(BTW that is not a typo). What does {...}[] indicate?

1
  • Please consider modifying the code in this question so as to constitute a minimal reproducible example which, when dropped into a standalone IDE like The TypeScript Playground, clearly demonstrates the issue you are facing. Such code should not have unrelated errors (e.g., public station function is not valid). This will allow those who want to help you to immediately get to work solving the problem without first needing to re-create it. And it will make it so that any answer you get is testable against a well-defined use case. Commented Apr 6, 2021 at 1:10

2 Answers 2

2

The sort parameter is optional as indicated by the ? but if you do supply one it must conform to the type specified to the right of the : which is of type { order: -1 | 1; sortBy: string }[]. That type means it is an array of objects whose shape is { order: -1 | 1; sortBy: string }.

The presence of ; is because in Typescript you can delimit fields of an object type by either ; or , interchangeably.

This is identical but may read clearer to you:

type SortSpecification = {
  order: -1 | 1;
  sortBy: string;
};

const func = (a: number, sort?: SortSpecification[]) => {
  /* ... */
}

And these would all be valid calls to it:

func(42);
func(42, [{ order: 1, sortBy: 'foo' }]);
func(42, [{ order: 1, sortBy: 'foo' }, { order: -1, sortBy: 'bar' }]);
Sign up to request clarification or add additional context in comments.

1 Comment

how would I specify that the order field should be a number or an array of object literals(without specifying any fields) would order: number | {}[] do the trick?
1

Let's break it down

{ order: -1 | 1; sortBy: string }[]

The brackets at the end indicate an array. So it's an array of something, where that something is

{ order: -1 | 1; sortBy: string }

The objects in the array must have an order field which is either 1 or -1 and a sortBy field which must be a string.

Finally, as you've pointed out, the ? after the argument name makes it optional. So sort is an optional array of objects which have two fields: order and sortBy.

I certainly understand your confusion, as this is a complicated type signature. If I was writing this, it'd probably make more sense to extract it into an interface or an object type.

type SortParam = { order: -1 | 1, sortBy: string }

SortParam[]

As for the comma versus the semicolon, don't sweat it. Typescript lets you interchange the two in several situations. Use the one you prefer and don't worry about it.

5 Comments

is ; interchangable in object definitions as well or type specifications?
how would I specify that the order field should be a number or an array of object literals(without specifying any fields) would order: number | {}[] do the trick?
The semicolon thing is just in types; TS tries to maintain compatibility with JS in a lot of situations, including object literals. As for your second question, the answer is union types. If you want that written out in more detail, it's probably worth asking a new question on here, for the sake of organization.
okay. Just one more clarification, is order: -1|1 also providing a default value? e.g. nothing was passed in the order field, then -1 would be the default value?
No, -1|1 means the value must be a number which is either -1 or 1. The order field, as written, is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.