Explanation
The nature of the keys
determines that keys[0]
in Typescript can be any key from the Config
object (typeof keys[0]
remains as keyof Config
== 'A' | 'B' | 'N'
).
Therefore, it is expected that the value assigned to your object
config: Partial<Config>
satisfies all possible values simultaneously.
In this scenario, using Partial<Config>
, the expected values are the intersection between string | undefined
and number | undefined
. Consequently, the only common type is undefined
.
It should be noted that without Partial<Config>
, the expected values would represent the intersection between string
and
number</code), which do not intersect and result in the type <code>never
(accepting no values).
Solution
If your array of keys remains constant and unchanging,
One solution may involve defining keys
as follows:
const keys = ["A", "B", "N"] as const;
This approach enables typescript to understand the precise value of
keys[0]
, allowing for accurate typing of the expected value.