analytics-next
analytics-next copied to clipboard
undefined as JSONValue
Hello 👋
Since https://github.com/segmentio/analytics-next/pull/561 we are unable to pass undefined as a value to track/identify calls as JSONValue does not accept undefined values
Meaning this kind of (pseudo) function
const trackUser = ({ userId, language }: { userId: string, language?: string }) => {
analytics.track(
'User',
{
userId,
language,
})
}
Does not work anymore whilst it used to be a valid way to pass optional values.
Filtering undefined values can be a solution but a quite heavy one when JSON.stringify automatically strip undefined values
Could undefined be added back as a possible values to objects ?
export type Traits = { [k: string]: JSONValue | undefined }
export type EventProperties = {
[k: string]: JSONValue | undefined
}
Thanks :)
Thanks @chambo-e for reporting this. This seems reasonable that we want to augment JSON to allow undefined properties, as we assume that users understand that any undefined values are removed on serialization.
In the meantime, you can fallback on null, as null is of course a valid JSON primitive (for anyone else who comes across this issue).
const trackUser = ({ userId, language = null }: { userId: string, language?: string | null }) => {
analytics.track( 'User', { userId, language })
}