-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: enforce type compatibility in schema mappings
- Add ValidSourceKey helper type to validate source/target property types - Restrict direct property mappings to compatible types only - Keep TransmuteFn functionality unchanged - Add example usage demonstrating type checking BREAKING CHANGE: Schema type now enforces strict type compatibility between source and target properties. Code with mismatched types in direct property mappings will need to be updated to use TransmuteFn for type conversion or fix the type mismatch.
- Loading branch information
Showing
5 changed files
with
66 additions
and
96 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,36 +1,43 @@ | ||
/** | ||
* Arguments passed to a mutation function | ||
* @template Source - The source type being transmuted from | ||
* @template TExtra - Type of additional data for transmutation | ||
* @template Extra - Type of additional data for transmutation | ||
*/ | ||
export type TransmuteFnArgs<Source, TExtra> = { | ||
export type TransmuteFnArgs<Source, Extra> = { | ||
/** The source object being transmuted */ | ||
source: Source | ||
/** Optional extra data to assist with transmutation */ | ||
extra?: TExtra | ||
extra?: Extra | ||
} | ||
|
||
/** | ||
* Function that performs a custom transmutation on a source object | ||
* @template Source - The source type being transmuted from | ||
* @template Target - The target type being transmuted to | ||
* @template TargetKey - The specific key of the target property being set | ||
* @template TExtra - Type of additional data for transmutation | ||
* @template Extra - Type of additional data for transmutation | ||
*/ | ||
export type TransmuteFn<Source, Target, TargetKey extends keyof Target, TExtra = unknown> = | ||
(args: TransmuteFnArgs<Source, TExtra>) => Target[TargetKey] | ||
export type TransmuteFn<Source, Target, TargetKey extends keyof Target, Extra> = | ||
(args: TransmuteFnArgs<Source, Extra>) => Target[TargetKey] | ||
|
||
/** | ||
* Get keys of Source that have values assignable to Target[TargetKey] | ||
*/ | ||
type AssignableKeys<Source, Target, TargetKey extends keyof Target> = { | ||
[SourceKey in keyof Source]: Source[SourceKey] extends Target[TargetKey] ? SourceKey : never | ||
}[keyof Source] | ||
|
||
/** | ||
* Defines how a property should be transmuted from source to target type | ||
* @template Source - The source type being transmuted from | ||
* @template Target - The target type being transmuted to | ||
* @template TExtra - Type of additional data for transmutation | ||
* @template Extra - Type of additional data for transmutation | ||
*/ | ||
export type Schema<Source, Target, TExtra = unknown> = { | ||
export type Schema<Source, Target, Extra = unknown> = { | ||
[TargetKey in keyof Target]: { | ||
/** Target property key */ | ||
to: TargetKey | ||
/** Source property key for direct mapping or a custom transmutation function */ | ||
from: keyof Source | TransmuteFn<Source, Target, TargetKey, TExtra> | ||
from: AssignableKeys<Source, Target, TargetKey> | TransmuteFn<Source, Target, TargetKey, Extra> | ||
} | ||
}[keyof Target] |