I'm trying to use typed object value as key for another object, but TS seems to loose its type and evaluate it as string.
Is it by design?
Are there any way to get targetObj
with keys' of type SomeKey
?
type SomeKey = "A" | "B"
const Obj = {
"A": "A"
} as { [k in SomeKey]: SomeKey }
type A = typeof Obj.A
// TargetObj ---> { [x: string]: string } instead of { [k in SomeKey]: SomeKey }
const TargetObj = {
[Obj.A]: "str"
}
EDIT
As Alexey L. correctly pointed out out the problem lies in difference between {[k in SomeKey]: SomeKey}
and {[k in SomeKey]: k}
But can anybody explain how it works?