Can we transform this code snippet into something like the following?
const array = ['a', 'b', 'c']; // this will change dynamically, may sometimes be ['a', 'e', 'f']
const readonlyArray = array as const;
type DesiredType = typeof readonlyArray[number];
I believe this might not be achievable, but is there a similar solution that could achieve the same functionality?
Here's a more simplified version of the original problem:
I have a React component with 2 props - status: 'a' | 'b'
, renamedPropB?: string
. When renamedPropB
transitions from undefined
to, for example, 'Z'
, the type of status
should transition from 'a' | 'b'
to 'a' | 'Z'
.
The aim is to store the string literals in the union in an array of strings so that it can easily be modified by changing or removing existing string literals from the union.