Dynamic Parameters using a single field named name
avoids type safety
#4742
-
I'm not sure if Discussions is the right place to post this, but since I just started using xstate, here goes! I was creating a dummy project for myself with no other dependencies than xstate 5.6.2 and typescript 5.3.3. I copy and pasted the following code snippet from the documentation at https://stately.ai/docs/typescript#dynamic-parameters import { setup } from 'xstate';
const feedbackMachine = setup({
types: {
context: {} as {
user: { name: string };
}
},
actions: {
greet: (_, params: { name: string }) => {
console.log(`Hello, ${params.name}!`);
}
}
}).createMachine({
context: {
user: {
name: 'David'
}
},
// ...
entry: {
type: 'greet',
params: ({ context }) => ({
name: context.user.name
})
}
}); Oddly, when I modified this section of the code
The Typescript compiler does not complain about the fact that I'm using a number instead of a string. In fact, the accepted type of params appears to be However, if I change the name of the parameter to anything BUT
My question is simply why this might be happening? It seems like it won't be a problem for me as long as none of my actions w/dynamic parameters use Note: I thought there might be some weird collision about a Note: I also tried out an action with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's because TS is a structural type system and functions... have I had a PR fixing this (here) but we decided to close it since this is not a super common problem and it would require us to do "weird things" to overcome this. Maybe we should revisit that decision though |
Beta Was this translation helpful? Give feedback.
It's because TS is a structural type system and functions... have
.name
property that is a string. So to hit this problem you specifically have to havename: string
in your params (where aname
's type usually would be astring
).I had a PR fixing this (here) but we decided to close it since this is not a super common problem and it would require us to do "weird things" to overcome this. Maybe we should revisit that decision though