Improved control over mapped type modifiers#21919
Merged
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mapped types currently support adding a
readonlyor?modifier to a mapped property, but they do not provide support the ability to remove modifiers. This matters in homomorphic mapped types (see #12563 and #12826) which by default preserve the modifiers of the underlying type.With this PR we provide the ability for a mapped type to either add or remove a particular modifier. Specifically, a
readonlyor?property modifier in a mapped type can now be prefixed with either+or-to indicate that the modifier should be added or removed.Using this ability, the PR defines a new
Required<T>type inlib.d.ts. This type strips?modifiers from all properties ofT, thus making all properties required:Some examples of
+and-on mapped type modifiers:A modifier with no
+or-prefix is the same as a modifier with a+prefix. So, theReadonlyPartial<T>type above corresponds toIn
--strictNullChecksmode, when a homomorphic mapped type removes a?modifier from a property in the underlying type it also removesundefinedfrom the type of that property:Fixes#15012.