3

I'm converting certain properties from an object to an array.

const result = ({ a, b }: { a: string, b: number }) => [a, b]; // (string | number)[]

However, the standard way always returns an insufficient typescript type (string | number)[]

I have to always specify my output type for it to correctly type the result.

const result = ({ a, b }: { a: string, b: number }): [string, number] => [a, b]; // [string, number]

Is there a simpler way?

3 Answers 3

4

Just add as const after the array is created - this is called a const assertion. It indicates that the newly created array is actually a tuple, which it has to be if you're going to count on it having 2 elements where the first is a string and the second is a number.

// readonly [string, number]
const result = ({ a, b }: { a: string, b: number }) => [a, b] as const; 

The only difference between this and the example in your original question is that resulting type will be readonly. There will be no way to alter the value of result unless you do a specific assertion that would allow this.

Sign up to request clarification or add additional context in comments.

Comments

1

As far as I know, there isn't.

You can do [a, b] as [string, number] but that's basically the same verbosity. Another alternative is giving result a function type, but that's just moving the "problem" here.

Besides, adding that small return type declaration isn't that bad. There isn't much else TS can do, as it can't assume you want a [string, number] by default. That would be far from backwards compatible (or even desired by most people).

Comments

1

There's no getting away from the fact that you have to add a little bit of verbosity to get a tuple type back. You can create a helper function which generates tuple types from array literals - whether it is preferable to declaring const or the return signature directly is up to you:

const asTuple = <T extends [any, ...any]>(array: T) => array;

const objectToTuple = ({ a, b }: { a: string, b: number }) => asTuple([a, b]);

const result = objectToTuple({a: "a", b: 1}) // [string, number]

Comments

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.