It would be greatly beneficial if the example code provided was a genuine minimum reproducible example showcasing the exact errors without any other mistakes or issues. However, I have an understanding of what may be occurring.
The primary issue that you are likely to encounter is the inference by the compiler that myList
is of type string[]
. This inference is reasonable on the part of the compiler because individuals often create arrays like const foo = ["a","b"]
with the intention of modifying the values within the array later (e.g., foo.push("c")
). A const
declaration in this context only restricts reassignment (foo = ...
) and does not prevent setting properties or invoking state-modifying methods on it.
To assist the compiler in cases where incorrect inference occurs on a literal value such as ["aaa","bbb","ccc"]
, one can utilize a const
assertion. By asserting
const myList = ['aaa','bbb','ccc'] as const
, the compiler will assign the type
readonly ["aaa", "bbb", "ccc"]
to
myList
. This type represents a tuple of length 3 with immutable values that are string literals
"aaa"
,
"bbb"
, and
"ccc"
in sequential order.
Subsequently, your code should mostly compile smoothly by ensuring that appContext
is asserted as an IAppContext
, for instance,
const appContext = {} as IAppContext
. It's essential to understand that simply annotating it like
const appContext: IAppContext = {}
is inaccurate as it starts empty and is not a valid
IAppContext
. Through a type assertion, you reassure the compiler that although
appContext
initiates as an invalid
IAppContext
, it will eventually become one after execution. Yet, it remains your responsibility to ensure proper initialization. For instance, if
myList
were just
['aaa','bbb']
, the code would compile without warnings, but issues might arise at runtime when calling
appContext.ccc.toUpperCase()
.
In conclusion, below is the revised error-free code:
interface IAppContext {
aaa: string;
bbb: string
ccc: string
}
const myList = ['aaa', 'bbb', 'ccc'] as const; // Constant context to retain literals
const appContext = {} as IAppContext; // Assertion due to initial emptiness
for (let i = 0; i < 3; i++) {
appContext[myList[i]] = 'newval' + i
}
I hope this explanation proves helpful. Best of luck!
Playground link to code