-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make variable key types overridable via module declaration (#1002)
Co-authored-by: SimeonC <[email protected]>
- Loading branch information
Showing
17 changed files
with
157 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { VariableTypeAlias, VariableValue } from './config/models' | ||
|
||
/** | ||
* Used to support strong typing of variable keys in the SDK. | ||
* Usage; | ||
* ```ts | ||
* import '@devcycle/types'; | ||
* declare module '@devcycle/types' { | ||
* interface CustomVariableDefinitions { | ||
* 'flag-one': boolean; | ||
* } | ||
* } | ||
* ``` | ||
* Or when using the cli generated types; | ||
* ```ts | ||
* import '@devcycle/types'; | ||
* declare module '@devcycle/types' { | ||
* interface CustomVariableDefinitions extends DVCVariableTypes {} | ||
* } | ||
* ``` | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
export interface CustomVariableDefinitions {} | ||
type DynamicBaseVariableDefinitions = | ||
keyof CustomVariableDefinitions extends never | ||
? { | ||
[key: string]: VariableValue | ||
} | ||
: CustomVariableDefinitions | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
export interface VariableDefinitions extends DynamicBaseVariableDefinitions {} | ||
export type VariableKey = string & keyof VariableDefinitions | ||
|
||
// type that determines whether the CustomVariableDefinitions interface has any keys defined, meaning | ||
// that we're using custom variable types | ||
export type CustomVariablesDefined = | ||
keyof CustomVariableDefinitions extends never ? false : true | ||
|
||
// type helper which turns a default value type into the type defined in custom variable types, if those exist | ||
// otherwise run it through VariableTypeAlias | ||
export type InferredVariableType< | ||
K extends VariableKey, | ||
DefaultValue extends VariableDefinitions[K], | ||
> = CustomVariablesDefined extends true | ||
? VariableDefinitions[K] | ||
: VariableTypeAlias<DefaultValue> |
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
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
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
Oops, something went wrong.