Skip to content

Commit

Permalink
Use Overloads for Index Signature
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Dec 4, 2024
1 parent 18df755 commit 3b3781d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 157 deletions.
11 changes: 7 additions & 4 deletions src/type/indexed/indexed-from-mapped-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ type TMappedIndexPropertyKey<Type extends TSchema, Key extends PropertyKey> = {
[_ in Key]: TIndex<Type, [Key]>
}
// prettier-ignore
function MappedIndexPropertyKey<Type extends TSchema, Key extends PropertyKey
>(type: Type, key: Key, options?: SchemaOptions): TMappedIndexPropertyKey<Type, Key> {
function MappedIndexPropertyKey<Type extends TSchema, Key extends PropertyKey>(type: Type, key: Key, options?: SchemaOptions): TMappedIndexPropertyKey<Type, Key> {
return { [key]: Index(type, [key], Clone(options)) } as never
}
// ------------------------------------------------------------------
Expand All @@ -55,7 +54,10 @@ type TMappedIndexPropertyKeys<Type extends TSchema, PropertyKeys extends Propert
: Result
)
// prettier-ignore
function MappedIndexPropertyKeys<Type extends TSchema, PropertyKeys extends PropertyKey[]>(type: Type, propertyKeys: [...PropertyKeys], options?: SchemaOptions): TMappedIndexPropertyKeys<Type, PropertyKeys> {
function MappedIndexPropertyKeys<
Type extends TSchema,
PropertyKeys extends PropertyKey[]
>(type: Type, propertyKeys: [...PropertyKeys], options?: SchemaOptions): TMappedIndexPropertyKeys<Type, PropertyKeys> {
return propertyKeys.reduce((result, left) => {
return { ...result, ...MappedIndexPropertyKey(type, left, options) }
}, {} as TProperties) as never
Expand All @@ -68,7 +70,8 @@ type TMappedIndexProperties<Type extends TSchema, MappedKey extends TMappedKey>
TMappedIndexPropertyKeys<Type, MappedKey['keys']>
>
// prettier-ignore
function MappedIndexProperties<Type extends TSchema, MappedKey extends TMappedKey>(type: Type, mappedKey: MappedKey, options?: SchemaOptions): TMappedIndexProperties<Type, MappedKey> {
function MappedIndexProperties<Type extends TSchema, MappedKey extends TMappedKey
>(type: Type, mappedKey: MappedKey, options?: SchemaOptions): TMappedIndexProperties<Type, MappedKey> {
return MappedIndexPropertyKeys(type, mappedKey.keys, options) as never
}
// ------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions src/type/indexed/indexed-from-mapped-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ type TFromProperties<Type extends TSchema, Properties extends TProperties> = (
function FromProperties<Type extends TSchema, Properties extends TProperties>(type: Type, properties: Properties, options?: SchemaOptions): TFromProperties<Type, Properties> {
const result = {} as Record<PropertyKey, TSchema>
for(const K2 of Object.getOwnPropertyNames(properties)) {
const keys = IndexPropertyKeys(properties[K2])
result[K2] = Index(type, keys, options) as never
result[K2] = Index(type, IndexPropertyKeys(properties[K2]), options)
}
return result as never
}
Expand Down
28 changes: 13 additions & 15 deletions src/type/indexed/indexed-property-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ import { IsTemplateLiteral, IsUnion, IsLiteral, IsNumber, IsInteger } from '../g
// FromTemplateLiteral
// ------------------------------------------------------------------
// prettier-ignore
type TFromTemplateLiteral<TemplateLiteral extends TTemplateLiteral,
Result extends string[] = TTemplateLiteralGenerate<TemplateLiteral>
> = Result
type TFromTemplateLiteral<TemplateLiteral extends TTemplateLiteral, Keys extends string[] = TTemplateLiteralGenerate<TemplateLiteral>> = (Keys)
// prettier-ignore
function FromTemplateLiteral<TemplateLiteral extends TTemplateLiteral>(templateLiteral: TemplateLiteral): TFromTemplateLiteral<TemplateLiteral> {
const result = TemplateLiteralGenerate(templateLiteral) as string[]
return result.map(S => S.toString()) as never
const keys = TemplateLiteralGenerate(templateLiteral) as string[]
return keys.map(key => key.toString()) as never
}
// ------------------------------------------------------------------
// FromUnion
Expand All @@ -59,9 +57,9 @@ type TFromUnion<Types extends TSchema[], Result extends string[] = []> = (
: Result
)
// prettier-ignore
function FromUnion<Type extends TSchema[]>(type: Type): TFromUnion<Type> {
function FromUnion<Types extends TSchema[]>(types: Types): TFromUnion<Types> {
const result = [] as string[]
for(const left of type) result.push(...IndexPropertyKeys(left))
for(const type of types) result.push(...IndexPropertyKeys(type))
return result as never
}
// ------------------------------------------------------------------
Expand All @@ -74,23 +72,23 @@ type TFromLiteral<LiteralValue extends TLiteralValue> = (
: []
)
// prettier-ignore
function FromLiteral<LiteralValue extends TLiteralValue>(T: LiteralValue): TFromLiteral<LiteralValue> {
function FromLiteral<LiteralValue extends TLiteralValue>(literalValue: LiteralValue): TFromLiteral<LiteralValue> {
return (
[(T as string).toString()] // TS 5.4 observes TLiteralValue as not having a toString()
[(literalValue as string).toString()] // TS 5.4 observes TLiteralValue as not having a toString()
) as never
}
// ------------------------------------------------------------------
// IndexedKeyResolve
// IndexPropertyKeys
// ------------------------------------------------------------------
// prettier-ignore
export type TIndexPropertyKeys<Type extends TSchema, Result extends PropertyKey[] = (
Type extends TTemplateLiteral ? TFromTemplateLiteral<Type> :
Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<Types> :
Type extends TLiteral<infer LiteralValue extends TLiteralValue> ? TFromLiteral<LiteralValue> :
export type TIndexPropertyKeys<Type extends TSchema> = (
Type extends TTemplateLiteral ? TFromTemplateLiteral<Type> :
Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<Types> :
Type extends TLiteral<infer Value extends TLiteralValue> ? TFromLiteral<Value> :
Type extends TNumber ? ['[number]'] :
Type extends TInteger ? ['[number]'] :
[]
)> = Result
)
/** Returns a tuple of PropertyKeys derived from the given TSchema */
// prettier-ignore
export function IndexPropertyKeys<Type extends TSchema>(type: Type): TIndexPropertyKeys<Type> {
Expand Down
Loading

0 comments on commit 3b3781d

Please sign in to comment.