Typescript Usage
The DevCycle Node.js SDK is written in Typescript and includes a full Typescript interface.
It is also possible to enhance the type safety of the SDK by using the Devcycle CLI to generate a type definition based on the complete set of variables defined in your project. Using this method, you can ensure that your code cannot access a variable key that is not defined in DevCycle, or treat a variable as a different type.
For example, if you have a variable with the key my-variable which is a string type, the following code would
produce a type error:
const user = { user_id: 'my_user' }
// type error, can't use a boolean default value since we know it's a string
const myVariableValue = devcycleClient.variableValue('my-variable', user, false)
...
// type error, can't use the unknown key 'some-key'
const myVariableValue = devcycleClient.variableValue('some-key', user, 'default-value')
...
// this works, since we know this key exists and is a string
const myVariableValue = devcycleClient.variableValue('my-variable', user, 'default-value')
Usage
To use this enhanced type-safety, you can define a type containing the variable keys and their types
type VariableTypes = {
'my-variable': string
}
You can then use declare module interface merging to augment the types the SDK uses:
declare module '@devcycle/types' {
interface CustomVariableDefinitions extends VariableTypes {}
}
You can alternatively pass the definition as a generic argument to the initializeDevCycle function:
const dvcOptions = { logLevel: 'debug' }
const devcycleClient = await initializeDevCycle<VariableTypes>(
'<DEVCYCLE_SERVER_SDK_KEY>',
dvcOptions,
).onClientInitialized()
The keys of VariableTypes must match the keys of the variables defined in DevCycle, and the values must match the
expected type of the variable.
You can write this definition manually, but it's recommended to generate it automatically as part of your build process by using the CLI.