I am currently working on defining a type for an array that requires specific values to be present in a certain order at the beginning of the array.
type SpecificArray = ('hello'|'goodbye'|string)[]
// Current
const myArray: SpecificArray = [] // valid
const myArray: SpecificArray = [''] // valid
const myArray: SpecificArray = ['something'] // valid
const myArray: SpecificArray = ['hello'] // valid
const myArray: SpecificArray = ['hello', 'goodbye'] // valid
const myArray: SpecificArray = ['hello', 'goodbye', 'something'] // valid
// Desired
const myArray: SpecificArray = [] // should fail
const myArray: SpecificArray = [''] // should fail
const myArray: SpecificArray = ['something'] // should fail
const myArray: SpecificArray = ['hello'] // should fail
const myArray: SpecificArray = ['hello', 'goodbye'] // okay
const myArray: SpecificArray = ['hello', 'goodbye', 'something'] // okay
I have tried multiple approaches, but none seem to give me the desired outcome...
type SpecificArray = ('hello'|'goodbye'|string)[]
/* ---- */
type SpecificArray = ['hello'|'goodbye'|string]
/* ---- */
import type { LiteralUnion } from 'type-fest'
type SpecificArray = LiteralUnion<'hello'|'goodbye', string>[]
Any help or suggestions would be greatly appreciated!