-
-
Notifications
You must be signed in to change notification settings - Fork 724
Add RenameKeys type
#1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
materwelonDhruv
wants to merge
22
commits into
sindresorhus:main
Choose a base branch
from
materwelonDhruv:add-rename-key
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+381
−0
Open
Add RenameKeys type
#1435
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d001faa
feat: add `RenameKey` type
materwelonDhruv 80c9aec
feat: add `RenameKeys` type
materwelonDhruv 0eddd05
fix(lint): lint error in import statement
materwelonDhruv 229c206
fix: reject key collisions and optional map entries in `RenameKey`/`R…
materwelonDhruv 9c3e2d3
tests: cover `RenameKey`/`RenameKeys` edge cases and rejections
materwelonDhruv 889d438
fix: linter needs enable statements after file-level disable statements
materwelonDhruv a407f21
fix: various issues
materwelonDhruv 92b4a86
chore: update readme
materwelonDhruv 6bf4632
fix: lint issues
materwelonDhruv d556969
feat: add reliable merging behavior
materwelonDhruv c90f79b
chore: remove `RenameKey` type
materwelonDhruv 4f71ea3
refactor: use the existing internal type for this
materwelonDhruv c4a9851
refactor: make option keys no-op instead of fail
materwelonDhruv 8da94ac
refactor: allow unions distributing their types on rename
materwelonDhruv c09e705
fix: correct modifier handling on merged targets
materwelonDhruv bbba36a
fix: unsynced readme desc
materwelonDhruv 231568a
fix: incorrect optional behavior with EOPT off
materwelonDhruv 3b6b979
fix: make non-literal targets no op instead of produce a `never`
materwelonDhruv 438f46f
refactor: simplify the type's impl + some more tests
materwelonDhruv 8615b9b
Merge remote-tracking branch 'upstream/main' into add-rename-key
materwelonDhruv aafd4ea
fix: updated API
materwelonDhruv 2b3db9a
refactor: the source value type is not needed anymore
materwelonDhruv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import type {IsLiteral} from './is-literal.d.ts'; | ||
| import type {ReadonlyKeysOf} from './readonly-keys-of.d.ts'; | ||
| import type {RequiredKeysOf} from './required-keys-of.d.ts'; | ||
| import type {OptionalKeysOf} from './optional-keys-of.d.ts'; | ||
| import type {OmitIndexSignature} from './omit-index-signature.d.ts'; | ||
| import type {SetRequired} from './set-required.d.ts'; | ||
| import type {SetReadonly} from './set-readonly.d.ts'; | ||
| import type {IfNotAnyOrNever, IsExactOptionalPropertyTypesEnabled} from './internal/type.d.ts'; | ||
|
|
||
| /** | ||
| Rename keys in an object type according to a map of old-to-new names. Keys absent from the map are returned unchanged. The value type, the optional modifier, and the readonly modifier are applied to the new key. | ||
|
|
||
| Distributes over a union of rename maps and over a union of source types. | ||
|
|
||
| When multiple source keys map to the same target, the target's value type is the union of the contributors' value types. The target is optional only when every contributor is optional, and is `readonly` when any contributor is `readonly`. With `exactOptionalPropertyTypes` disabled, the value type of a mixed-optionality merge also includes `undefined`. | ||
|
|
||
| A union target distributes, producing one output key per member. For example, `{a: 'b' | 'c'}` on a source with `a: string` produces both `b: string` and `c: string`. | ||
|
|
||
| A rename map entry whose key is not a property of the source type is ignored, matching the result of `Omit`. An entry whose value is not a literal `PropertyKey` (such as `string`) is also ignored, leaving that key's name unchanged. The optional modifier on a rename map entry is ignored, so `{a?: 'alpha'}` produces the same result as `{a: 'alpha'}`. | ||
|
|
||
| An `any` source returns `any`, and a `never` source returns `never`. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {RenameKeys} from 'type-fest'; | ||
|
|
||
| type User = { | ||
| id: string; | ||
| firstName: string; | ||
| createdAt: Date; | ||
| }; | ||
|
|
||
| type Renamed = RenameKeys<User, {firstName: 'first_name'; createdAt: 'created_at'}>; | ||
| //=> {id: string; first_name: string; created_at: Date} | ||
| ``` | ||
|
|
||
| @example | ||
| ``` | ||
| import type {RenameKeys} from 'type-fest'; | ||
|
|
||
| type SearchInput = { | ||
| textQuery: string; | ||
| voiceQuery: Blob; | ||
| imageQuery: File; | ||
| }; | ||
|
|
||
| type Normalized = RenameKeys<SearchInput, {textQuery: 'query'; voiceQuery: 'query'; imageQuery: 'query'}>; | ||
| //=> {query: string | Blob | File} | ||
| ``` | ||
|
|
||
| @category Object | ||
| */ | ||
| export type RenameKeys< | ||
| BaseType extends object, | ||
| RenameMap extends Record<PropertyKey, PropertyKey>, | ||
| > = IfNotAnyOrNever<BaseType, { | ||
| ifNot: BaseType extends unknown // Distribute over a union source | ||
| ? RenameMap extends unknown // Distribute over a union map | ||
| ? _RenameOnce<BaseType, _NormalizeMap<RenameMap>> | ||
| : never | ||
| : never; | ||
| }>; | ||
|
|
||
| type _NormalizeMap<RenameMap extends Record<PropertyKey, PropertyKey>> = { | ||
| -readonly [Key in keyof RenameMap as true extends IsLiteral<RenameMap[Key]> ? Key : never]-?: RenameMap[Key]; | ||
| }; | ||
|
|
||
| type _TargetOf<SourceKey extends PropertyKey, RenameMap extends Record<PropertyKey, PropertyKey>> = | ||
| SourceKey extends keyof RenameMap ? RenameMap[SourceKey] : SourceKey; | ||
|
|
||
| type _RenameOnce<BaseType extends object, RenameMap extends Record<PropertyKey, PropertyKey>> = | ||
| _RestoreMergedUndefined<BaseType, RenameMap, | ||
| _ApplyReadonly<BaseType, RenameMap, | ||
| _ApplyRequired<BaseType, RenameMap, | ||
| _RenameNaive<BaseType, RenameMap>>>>; | ||
|
|
||
| type _RenameNaive<BaseType extends object, RenameMap extends Record<PropertyKey, PropertyKey>> = { | ||
| // Two keys mapping to one target produce a union value and keep only the first key's modifiers | ||
| // Like for example, {a?: 1; b: 2} with {a: 'x'; b: 'x'} produces {x?: 1 | 2}, taking a's optional | ||
| [Key in keyof BaseType as _TargetOf<Key, RenameMap>]: _SourceValue<BaseType, Key>; | ||
|
materwelonDhruv marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| type _SourceValue<BaseType extends object, Key extends keyof BaseType> = | ||
| // EOPT on: Required drops the implicit undefined from an optional value | ||
| (IsExactOptionalPropertyTypesEnabled extends true ? Required<BaseType> : BaseType)[Key]; | ||
|
|
||
| // A merged target kept only one modifier in _RenameNaive, so re-force these from every contributor. | ||
| type _ApplyRequired<BaseType extends object, RenameMap extends Record<PropertyKey, PropertyKey>, Renamed> = | ||
| SetRequired<Renamed, _TargetOf<RequiredKeysOf<OmitIndexSignature<BaseType>>, RenameMap> & keyof Renamed>; | ||
|
|
||
| type _ApplyReadonly<BaseType extends object, RenameMap extends Record<PropertyKey, PropertyKey>, Renamed> = | ||
| SetReadonly<Renamed, _TargetOf<ReadonlyKeysOf<OmitIndexSignature<BaseType>>, RenameMap> & keyof Renamed>; | ||
|
|
||
| type _MergedTargets<BaseType extends object, RenameMap extends Record<PropertyKey, PropertyKey>> = | ||
| // Targets renamed from both a required and an optional key prefer the required modifier | ||
| // Required b and optional a both rename to x in {a?: 1; b: 2} with {a: 'x'; b: 'x'} | ||
| _TargetOf<RequiredKeysOf<OmitIndexSignature<BaseType>>, RenameMap> | ||
| & _TargetOf<OptionalKeysOf<OmitIndexSignature<BaseType>>, RenameMap>; | ||
|
|
||
| // EOPT off keeps undefined on a target that merged a required and an optional source | ||
| // Source {a?: 1; x: 2} with {a: 'x'} gives {x: 1 | 2 | undefined} | ||
| type _RestoreMergedUndefined<BaseType extends object, RenameMap extends Record<PropertyKey, PropertyKey>, Result> = | ||
| IsExactOptionalPropertyTypesEnabled extends true | ||
| ? Result | ||
| : { | ||
| [Key in keyof Result]: Key extends _MergedTargets<BaseType, RenameMap> | ||
| ? Result[Key] | undefined | ||
| : Result[Key]; | ||
| }; | ||
|
|
||
| export {}; | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.