-
I know the type of "props.G" is defined by the project, and "props.ctx" is "Ctx", but what is the type of "props.moves"?
Currently I am using "any" but this is not a safe way at all. Also, it's quite appreciated to add some examples and documentations on using boardgame.io with typescript. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Here's a small example using Typescript, but yes proper TS documentation is still needed. We don't have strongly typed moves, although it might be theoretically possible to compute them from a game definition, so for now type moves = Record<
string,
(...args: any[]) => void
>; If you follow the example linked above though you shouldn't have to type this manually. If you specifically need the moves type you can use |
Beta Was this translation helpful? Give feedback.
-
@delucis Thanks, this demo is quite helpful. Maybe it's impossible for typescript now to give a 100% type check. |
Beta Was this translation helpful? Give feedback.
-
@jakehsiao It’s tricky. If you declare your game as I’m not sure the trade-off of typing everything manually is worth it, even though having auto-completion and type safety on the client would be nice. |
Beta Was this translation helpful? Give feedback.
-
@delucis I believe one way to get the best of both worlds (detailed type information + automatic type checking) would be to use an identity function with a generic type signature, like so: function defineGame<Gm extends Game>(game: Gm): Gm {
return game;
}
// example usage - MyGame will have detailed type information while ensuring it complies with the Game type.
const MyGame = defineGame({
moves: {
...
},
}); Then you could use the experimental type linked in your answer to get the type of Vue uses this pattern for similar reasons with their |
Beta Was this translation helpful? Give feedback.
Here's a small example using Typescript, but yes proper TS documentation is still needed.
We don't have strongly typed moves, although it might be theoretically possible to compute them from a game definition, so for now
moves
isIf you follow the example linked above though you shouldn't have to type this manually. If you specifically need the moves type you can use
BoardProps['moves']
.