I try to create a read-only tuple type with optional elements. The list can be of any length and contain any of the three elements 'a'
, 'b'
, and 'c'
. They are Literal Types, not string
type.
This is my solution by now, it seems to work. This method has a disadvantage. If I have 100 different literal types, I need to add an optional symbol ?
after each element, which is very cumbersome
export interface FormAttributesConfig {
list: readonly ['a'?, 'b'?, 'c'?];
}
const attr1: FormAttributesConfig = {
list: ['a', 'b', 'c']
}
const attr2: FormAttributesConfig = {
list: ['a', 'b']
}
const attr3: FormAttributesConfig = {
list: []
}
UPDATE:
I can use Partial
utility types to make all literal types in tuple optional.
export interface FormAttributesConfig {
list: Partial<readonly ['a', 'b', 'c']>
}
I want to know if there are any disadvantages in this way or a better way?