I am attempting to develop a type conversion method that transforms an array of strings into an object with the string values as keys.
type ObjectFromKeys<T extends Array<string>> = {
[K in keyof T]: string
}
declare const o: ObjectFromKeys<['foo']>
o.foo // Property 'foo' does not exist on type '[string]'
It appears that this modification works:
type ObjectFromKeys<T extends Array<string>> = {
[K in T as any]: string
}
This approach seems risky, is it the correct way to achieve this?
However, there is no error thrown if the accessed value is not present in the array
type ObjectFromKeys<T extends Array<string>> = {
[K in T as any]: string
}
declare const o: ObjectFromKeys<['foo']>
o.bar // This should fail