add plugin types to ctx interface#579
Conversation
|
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
|
@googlebot I signed it! |
|
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
|
I think it might be a good idea to make some of the types generics, so that users can do things like: type CtxCopy = Ctx; // has a default value with default plugins enabled
type CustomCtx = Ctx<{}>; // has no plugins enabled
type ExtendedCtx = Ctx<DefaultPlugins & { x: value }>; // extends defaultSimilarly for // CustomState['G'] will have type MyG instead of any
type CustomState = State<MyG, CustomCtx>;It should then be possible for users to do something like the following and type check as expected: type MyG = { val: boolean };
const myGame: GameConfig<MyG, CustomCtx> = {
endIf: G => G.field, // error: field does not exist in type MyG
};I need to check how would be best to implement this, but the types for Koa work like this. |
|
@delucis I think this should work for the export interface CtxBase {
numPlayers: number;
playOrder: Array<PlayerID>;
playOrderPos: number;
activePlayers: null | ActivePlayers;
currentPlayer: PlayerID;
numMoves?: number;
gameover?: any;
turn: number;
phase: string;
_activePlayersMoveLimit?: object;
_activePlayersNumMoves?: object;
_prevActivePlayers?: Array<object>;
_random?: {
seed: string | number;
};
}
export type Ctx<
T = { events: EventsAPI; random: RandomAPI }
> = CtxBase & T;However, I guess this will break typing inside Edit: Realized that the player Plugin is not active by default, so I removed it. |
Yes, it’ll need a bit of reorganising. The type Ctx<Plugins extends { [plugin: string]: any } = {}> = {
numPlayers: number;
// etc.
} & Plugins;
type DefaultPlugins = {
events: EventsAPI;
random: RandomAPI;
};
type CtxWithPlugins = Ctx<DefaultPlugins>;It probably still needs some wider consideration to work out how to cascade the types down to all the bits of code that need them. |
Maybe keep the changes simple for this PR and work on that on a follow-up PR? |
Sounds good to me! |
* add plugin types to ctx interface * extract plugin interfaces into corresponding plugin files * add missing state field in PlayerAPI interface * fix type errors in player plugin api() * use events return type void Co-authored-by: Nicolo John Davis <[email protected]>
Checklist
master).When using the typings of
CtxI noticed it was missing the events. After having a look into the source code, I realized therandomandplayerplugins also enhance theCtxinterface.The name plugin implies that they are not necessarily defined, so I declared them optional.
Also it seems like specific event functions can be deactivated, so I declared those functions all optional as well. This has the downside, that you need to add checks or non-null assertions to your typescript code when calling those event functions.