#Array and as const

4 messages · Page 1 of 1 (latest)

karmic birch
#

Is it possible to create both an array of strings AND apply as const?

This is how I normally create the type

type Method = (typeof METHODS)[number]```

But I also need to use the full list of `METHODS` as a normal array to pass into another function IE:
```const doTest = (methods: Method[]) => {}
doTest(METHODS)```

but that gives the following error:
`Argument of type 'readonly ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"]' is not assignable to parameter of type '("GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS")[]'.`
royal compass
#

does doTest actually mutate the methods array? if not you can change its signature to this:

const doTest = (methods: readonly Method[]) => {}
karmic birch
#

Yea that does work as I don't intend to mutate it right now at least.

toxic matrix
#

!resolved