Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined as JSONValue #570

Closed
chambo-e opened this issue Aug 8, 2022 · 1 comment · Fixed by #576
Closed

undefined as JSONValue #570

chambo-e opened this issue Aug 8, 2022 · 1 comment · Fixed by #576
Assignees

Comments

@chambo-e
Copy link

chambo-e commented Aug 8, 2022

Hello 👋

Since #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 :)

@silesky
Copy link
Contributor

silesky commented Aug 8, 2022

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 })
}
  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants