Created
May 29, 2019 14:28
-
-
Save NoFishLikeIan/587cadf433b4a93a5485850b05b12335 to your computer and use it in GitHub Desktop.
Structural config alternatives
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
type UnionizeArray<D extends Readonly<Array<string>>> = D[number] | |
const dimensions = ['x', 'y'] as const | |
const optionalDimensions = ['color', 'radius'] as const | |
type numerical = 'x' | 'y' | 'radius' | |
type _ = 'man' | 'opt' | |
type __ = 'num' | 'nonum' | |
interface D { | |
x: ['num', 'man'] | |
} | |
type NonEmptyArray<T> = Array<T> & { 0: T } | |
type RequiredDimensions = UnionizeArray<typeof dimensions> | |
type OptionalDimensions = UnionizeArray<typeof optionalDimensions> | |
type Dimensions = RequiredDimensions | OptionalDimensions | |
type VariableSettings<D extends Record<string, unknown>, K> = K extends numerical | |
? { | |
variable: keyof D | |
domain: [number, number] | |
} | |
: { | |
variable: keyof D | |
} | |
type OptionalStructureConfig<D extends Record<string, unknown>> = { | |
[K in OptionalDimensions]: K extends numerical | |
? Array<VariableSettings<D, K>> | |
: Array<VariableSettings<D, K>> | |
} | |
export const defaultOptionalStruct = <D>() => | |
optionalDimensions.reduce<OptionalStructureConfig<D>>( | |
(acc, k) => ({ ...acc, [k]: [] }), | |
{} as OptionalStructureConfig<D> | |
) | |
export type StructureConfig<D extends Record<string, unknown>> = { | |
[K in Dimensions]: K extends OptionalDimensions | |
? K extends numerical | |
? Array<VariableSettings<D, K>> | |
: Array<VariableSettings<D, K>> | |
: K extends numerical | |
? NonEmptyArray<VariableSettings<D, K>> | |
: Array<VariableSettings<D, K>> | |
} | |
// Alternative approach -------------------------------------- | |
type X = { | |
variable: string | |
domain: [number, number] | |
} | |
type Y = { | |
variable: string | |
domain: [number, number] | |
} | |
type Radius = { | |
variable: string | |
domain: [number, number] | |
} | |
type StructureConfig1 = { | |
x: NonEmptyArray<X> | |
y: NonEmptyArray<Y> | |
radius: Array<Radius> | |
// .... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment