ave
ave

Reputation: 19443

How do I declare a return type of a tuple of two numbers?

When I declare a function as

const coordinates = (id: number): ([number, number]) => {

the error I get is [ts] Duplicate identifier 'number'.

If I omit type signature for return value, then it infers it as number[]

Upvotes: 38

Views: 43633

Answers (2)

Alexandre Hitchcox
Alexandre Hitchcox

Reputation: 2753

const coordinates = (id: number) => [id, id] as const;
// const coordinates: (id: number) => [number, number]

From TypeScript 3.4 onwards you can use const assertations.

Upvotes: 25

Tom Cumming
Tom Cumming

Reputation: 1168

const coordinates = (id: number): [number, number] => [id, id];

No need for the parenthesis around the return tuple type

Upvotes: 46

Related Questions