Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/types/mapper.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expectTypeOf, test } from 'vitest'
import { Mapper } from '@/types/mapper'
import { Profile } from '@/types/profile'

declare const broadSourceMapper: Mapper<[
Profile<string, number, 'x', string>,
Profile<'a', string, 'y', boolean>
]>

declare const broadSourceKey: string

test('broad source keys do not expose destinations from unrelated profiles', () => {
// @ts-expect-error Only source a has destination y.
broadSourceMapper.map('b', 'hello', 'y')
// @ts-expect-error Only source a has destination y.
broadSourceMapper.mapMany('b', ['hello'], 'y')

expectTypeOf(broadSourceMapper.map('a', 'hello', 'y')).toEqualTypeOf<boolean>()
expectTypeOf(broadSourceMapper.mapMany('a', ['hello'], 'y')).toEqualTypeOf<boolean[]>()
expectTypeOf(broadSourceMapper.map(broadSourceKey, 1, 'x')).toEqualTypeOf<string>()
expectTypeOf(broadSourceMapper.mapMany(broadSourceKey, [1], 'x')).toEqualTypeOf<string[]>()

// @ts-expect-error Destination y expects a string input.
broadSourceMapper.map('a', 1, 'y')
// @ts-expect-error Destination y expects string inputs.
broadSourceMapper.mapMany('a', [1], 'y')
})

declare const literalMapper: Mapper<[
Profile<'a', number, 'x', string>,
Profile<'a', string, 'y', Promise<boolean>>,
Profile<'b', number, 'x', boolean>
]>

declare const unionSourceKey: 'a' | 'b'

test('literal and union keys retain their mapping results', () => {
expectTypeOf(literalMapper.map('a', 1, 'x')).toEqualTypeOf<string>()
expectTypeOf(literalMapper.map('a', 'hello', 'y')).toEqualTypeOf<Promise<boolean>>()
expectTypeOf(literalMapper.mapMany('a', ['hello'], 'y')).toEqualTypeOf<Promise<boolean>[]>()
expectTypeOf(literalMapper.map(unionSourceKey, 1, 'x')).toEqualTypeOf<string | boolean>()
expectTypeOf(literalMapper.mapMany(unionSourceKey, [1], 'x')).toEqualTypeOf<(string | boolean)[]>()

// @ts-expect-error Source b has no destination y.
literalMapper.map('b', 'hello', 'y')
// @ts-expect-error Destination x expects a number input.
literalMapper.map('a', 'hello', 'x')
})
16 changes: 9 additions & 7 deletions src/types/mapper.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { ExtractSourceKeys, Profile } from '@/types/profile'

/**
* Registered profiles grouped once by source key, then destination key, so each `map` call is two
* property lookups rather than a fresh pass over every registered profile.
* Type-level index of profiles by source and destination key, reused when checking mapping calls.
*/
type ProfileIndex<TProfile> = {
[TSourceKey in ExtractSourceKeys<TProfile>]: {
[TMatch in Extract<TProfile, { sourceKey: TSourceKey }> as TMatch extends Profile ? TMatch['destinationKey'] : never]: TMatch
}
}

// A broad source key can collapse index entries together. Check the selected source key
// before extracting types so unrelated profiles cannot become valid matches.

/** Extracts the source type from a profile. `unknown` for the destination parameter matches any profile regardless of its destination type. */
type ProfileSource<TProfile> = TProfile extends Profile<string, infer TSource, string, unknown> ? TSource : never
type ProfileSource<TProfile, TSourceKey extends string> = TProfile extends Profile<TSourceKey, infer TSource, string, unknown> ? TSource : never

/** Extracts the destination type from a profile. `never` for the source parameter exploits contravariance — any concrete source type satisfies `never extends TSource` in the function position, so the match is unconditional. */
type ProfileDestination<TProfile> = TProfile extends Profile<string, never, string, infer TDestination> ? TDestination : never
/** Extracts the destination type from a profile. `never` for the source parameter exploits contravariance — any concrete source type satisfies `never extends TSource` in the function position, so the source type does not restrict the match. */
type ProfileDestination<TProfile, TSourceKey extends string> = TProfile extends Profile<TSourceKey, never, string, infer TDestination> ? TDestination : never

export type Mapper<TProfiles extends readonly Profile[], TProfile = TProfiles[number]> = {
register: (profiles: Profile[] | readonly Profile[] | Profile) => void,
Expand All @@ -23,9 +25,9 @@ export type Mapper<TProfiles extends readonly Profile[], TProfile = TProfiles[nu
map: <
TSourceKey extends keyof ProfileIndex<TProfile> & string,
TDestinationKey extends keyof ProfileIndex<TProfile>[TSourceKey] & string
> (sourceKey: TSourceKey, source: ProfileSource<ProfileIndex<TProfile>[TSourceKey][TDestinationKey]>, destinationKey: TDestinationKey) => ProfileDestination<ProfileIndex<TProfile>[TSourceKey][TDestinationKey]>,
> (sourceKey: TSourceKey, source: ProfileSource<ProfileIndex<TProfile>[TSourceKey][TDestinationKey], TSourceKey>, destinationKey: TDestinationKey) => ProfileDestination<ProfileIndex<TProfile>[TSourceKey][TDestinationKey], TSourceKey>,
mapMany: <
TSourceKey extends keyof ProfileIndex<TProfile> & string,
TDestinationKey extends keyof ProfileIndex<TProfile>[TSourceKey] & string
> (sourceKey: TSourceKey, sourceArray: ProfileSource<ProfileIndex<TProfile>[TSourceKey][TDestinationKey]>[], destinationKey: TDestinationKey) => ProfileDestination<ProfileIndex<TProfile>[TSourceKey][TDestinationKey]>[],
> (sourceKey: TSourceKey, sourceArray: ProfileSource<ProfileIndex<TProfile>[TSourceKey][TDestinationKey], TSourceKey>[], destinationKey: TDestinationKey) => ProfileDestination<ProfileIndex<TProfile>[TSourceKey][TDestinationKey], TSourceKey>[],
}
1 change: 0 additions & 1 deletion src/types/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ export type ProfileKey<T extends Profile> = `${T['sourceKey']}-${T['destinationK
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ExtractSourceKeys<TProfile> = TProfile extends Profile<infer TSourceKey, any, any> ? TSourceKey : never


export type Profiles = readonly Profile[]
Loading