Repository navigation
Mapped type optional -> required property issue #13224
Description
Activity
JakeGinnivan commented
on Dec 30, 2016 AuthorMore actionsAs a side note, it would be awesome if you could iterate at runtime on the keys of an interface. I guess that starts moving into the reflection questions, but it would be super handy if code could be emitted based on type information. So not runtime reflection but static templating.
I probably need to generate the styleguide from the typescript source rather than the approach I am taking.
Mapped types that are a homomorphic transformation are treated specially in the type system. the above type
PropDescriptionsis one of such. The type system knows thatPropDescriptions<T>is a transformation ofT, and it copies thereadonlyand?modifiers on the new type as it is generated from the source. Think ofReadOnly<T>, whereThas optional properties, you would expect the result to have optional properties as well.You however change the mapping to make it a non-homomorphic transformation as such:
type PropDescriptions<T, K extends string> = { [P in K]: string }
and you would instantiate it with:
const expected: PropDescriptions<ThingProps, keyof ThingProps> = ...
Notice the constraint on
Kisstring; this way the type system can not assert that the result of this transformation is a homomorphic mapping onT, and thus no modifiers are copied through.It is worth noting that if the constraint was
K extends keyof T(e.g. the definition of typePickin lib.d.ts`), the transformation would still be considered a homomorphic one.- addedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
on Dec 30, 2016 - added a commit that references this issue
on Dec 31, 2016 JakeGinnivan commented
on Dec 31, 2016 AuthorMore actionsThanks, have opened a PR in the docs which adds some of this information. Appreciate the good explanation.
JakeGinnivan commented
on Dec 31, 2016 AuthorMore actionsIt would also be good to extend the docs to explain how to use the
PickandRecordtypes.Reacted by Aluan Haddad, Sebastian Nemeth, Vitaly Blinovskov, Maksim Kulichenko and Boris Berman- added a commit that references this issue
on Sep 28, 2017 - locked and limited conversation to collaborators
on Jun 19, 2018
TypeScript Version: 2.1.4
Code
https://github.com/JakeGinnivan/TypeScriptMappedTypeIssue
Expected behavior:
The mapped type
Is marking all the properties as required, I would expect this to fail:
with
Property 'prop2' is missing in type {...}Actual behavior:
No compilation error