Closed predragnikolic closed 2 years ago
This is for the same reason Object.keys()
returns string[]
:
https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript
Your PersonalityRecord
object isn't guaranteed (by the type system) to only contain Personality
keys, and in turn those extra properties are not guaranteed to be of type Result
.
Duplicate of https://github.com/microsoft/TypeScript/issues/35847
Thanks @fatcerberus , now it is clear why typescript behaves like this.
Having an Exact type would solve my issue, but until something like that exist I would have to stick with casting.
Have a nice day. π
Bug Report
Given the following code:
π Actual behavior
TypeScirpt is reporting an error.
Typescript thinks that
personality
is of typestring
, but I expect it to be of typePersonality
.π Expected behavior
Typescript should not report the error and the above code should just work.
Here is the CodeSandBox example: https://codesandbox.io/s/young-night-516gs?file=/src/index.ts:268-346
My current workaround is to use a cast
const result = personalityRecord[personality as Personality]
, but I don't like that. Is there a more nicer way to solve this?