1

I have an array of numbers like

const arr:number[] = [0,12,14,18,24,36]

that I want to assign custom keys to

arr.foo = 126

However, when I do this typescript tells me Property 'foo' does not exist on type 'number[]'

How should I type my Array to make this work?

1 Answer 1

1

you can create an interface to solve this

interface MyType<T> extends Array<T> {
    foo ?:number
}

const arr:MyType<number>= [0,12,14,18,24,36];

arr.foo = 12
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.