|
| 1 | +import { VariableTypeAlias, VariableValue } from './config/models' |
| 2 | + |
| 3 | +/** |
| 4 | + * Used to support strong typing of variable keys in the SDK. |
| 5 | + * Usage; |
| 6 | + * ```ts |
| 7 | + * import '@devcycle/types'; |
| 8 | + * declare module '@devcycle/types' { |
| 9 | + * interface CustomVariableDefinitions { |
| 10 | + * 'flag-one': boolean; |
| 11 | + * } |
| 12 | + * } |
| 13 | + * ``` |
| 14 | + * Or when using the cli generated types; |
| 15 | + * ```ts |
| 16 | + * import '@devcycle/types'; |
| 17 | + * declare module '@devcycle/types' { |
| 18 | + * interface CustomVariableDefinitions extends DVCVariableTypes {} |
| 19 | + * } |
| 20 | + * ``` |
| 21 | + */ |
| 22 | +// eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 23 | +export interface CustomVariableDefinitions {} |
| 24 | +type DynamicBaseVariableDefinitions = |
| 25 | + keyof CustomVariableDefinitions extends never |
| 26 | + ? { |
| 27 | + [key: string]: VariableValue |
| 28 | + } |
| 29 | + : CustomVariableDefinitions |
| 30 | +// eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 31 | +export interface VariableDefinitions extends DynamicBaseVariableDefinitions {} |
| 32 | +export type VariableKey = string & keyof VariableDefinitions |
| 33 | + |
| 34 | +// type that determines whether the CustomVariableDefinitions interface has any keys defined, meaning |
| 35 | +// that we're using custom variable types |
| 36 | +export type CustomVariablesDefined = |
| 37 | + keyof CustomVariableDefinitions extends never ? false : true |
| 38 | + |
| 39 | +// type helper which turns a default value type into the type defined in custom variable types, if those exist |
| 40 | +// otherwise run it through VariableTypeAlias |
| 41 | +export type InferredVariableType< |
| 42 | + K extends VariableKey, |
| 43 | + DefaultValue extends VariableDefinitions[K], |
| 44 | +> = CustomVariablesDefined extends true |
| 45 | + ? VariableDefinitions[K] |
| 46 | + : VariableTypeAlias<DefaultValue> |
0 commit comments