0

I'm trying to learn TypeScript (2.1) by creating a dice roller. I figured I'd use tuples to match up the side rolled with a picture. For example, if I roll a 1 (using a random number generator I plan on implementing), I get back the representation for a circle.

class D6 {
    values: [number, string];
    values = [1, "Circle"];
}

However, just with that little bit of code, I am getting this error:

Subsequent variable declarations must have the same type. Variable 'values' must be of type '[number, string]', but here has type '(string | number)[]'.

Is it possible to use tuples in a class in TypeScript?

1

1 Answer 1

5

It should be:

class D6 {
    values: [number, string] = [1, "Circle"];
}

In your code you are defining two properties with the same name (values).

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

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.