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

Argument of type Record<string, string> is not assignable to parameter of type Traits | Callback | null | undefined #575

Closed
zygopleural opened this issue Aug 10, 2022 · 6 comments · Fixed by #579

Comments

@zygopleural
Copy link

zygopleural commented Aug 10, 2022

Breaking change introduced

I have a breaking change trying to upgrade 1.41.0 -> 1.42.1

Expected

I should be able to pass any Record<string, string | number | JSONValue> as traits into the identify call.

Actual

I can only only pass Record<string, JSONValue>

Culprit

Looks like here was a breaking change in @segment/analytics-next@1.42.0

I believe #561 is the culprit, specifically these changes

@silesky
Copy link
Contributor

silesky commented Aug 10, 2022

@zygopleuralI Thanks for taking out an issue. I wasn't able to reproduce --

// This works
const traits: Record<string, string> =  {name: "hello"}
analyics.identify("foo", traits) 

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgQQHYEMA2BPGwDGAzgEJQQDuhAplADQoY55FwC+cAZmSHAOQAC1AOYgqqGAHp0jXAUIBaVFQAeMXgChNm-BFSF4MKOmAxCALjgAlKjqgATADz6owVEPrPXQgHxwAvHCIGKIWAEQAFlSYmBChrADc6gAUCOzohAxYskQAlAB0wHZieBzYSaEcELH0hsamOVpAA

FYI, the definition of JSONValue already contains string | number, so Record<string, JSONValue> is the same as Record<string, string | number | JSONValue>

// This works
const traits: Record<string, JSONValue | string | number> =  {name: "hello"};
analytics.identify("foo", traits)

Is there a more advanced type you're having problems with?

@zygopleural
Copy link
Author

const { id, picture, signInMethod, ...traits } = user
identify(id, traits)

I'm seeing

Screenshot 2022-08-10 at 16 41 02

Even if I do

const t = traits as Traits

Screenshot 2022-08-10 at 16 42 33

@silesky
Copy link
Contributor

silesky commented Aug 10, 2022

@zygopleural Thank you for the detail, that's an interesting one, and will be a problem if anyone attempts to coerce an object created with that pattern to a Record type. For now, you can fix by casting traits to "object" or by creating a UserTraits type like this:

type UserTraits = {
  name: string
  age: string
}
const { id, picture, signInMethod, ...traits } = user
identify(id, traits as UserTraits)

@silesky
Copy link
Contributor

silesky commented Aug 10, 2022

@zygopleural fixed in 1.42.2 1.42.3

@silesky
Copy link
Contributor

silesky commented Aug 11, 2022

FYI @zygopleural there is an open Typescript issue about this scenario / bug: microsoft/TypeScript#48014

@zygopleural
Copy link
Author

FYI @zygopleural there is an open Typescript issue about this scenario / bug: microsoft/TypeScript#48014

Ah nice find!

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