boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
10k stars 704 forks source link

Expose Typescript types #576

Closed delucis closed 4 years ago

delucis commented 4 years ago

@nicolodavis Following on from https://github.com/nicolodavis/boardgame.io/issues/573#issuecomment-603237109

For now, the bundle doesn’t include a boardgame.io/types package, so I can’t do

import X from 'boardgame.io/types'

I found the base database type by doing this, but that seems pretty fragile:

import { Async } from 'boardgame.io/dist/types/src/server/db/base'

I’m not familiar yet with how best to expose and bundle types. I think the package.json files generated for core, multiplayer etc. should include a types field pointing to the correct place for general consumption (TS docs).

Then when someone does import X from 'boardgame.io/X' Typescript will also know where to find the types for that module. (The server module doesn’t currently follow this pattern, so it might need a different approach?)

I’m also not sure what’s the nicest way to expose base.ts (because it’s not part of the main public-facing API), but I think it and all the related types should be somewhere like a server-internals package like the internal package, so database connectors can use them cleanly. (The Async abstract class is not only its types, because it actually provides functionality.)

nicolodavis commented 4 years ago

I think the package.json files generated for core, multiplayer etc. should include a types field pointing to the correct place for general consumption

You're right. The fix is relatively straightforward:

  1. We need to convert everything in packages/*.js to TS.

After this, dist/types/packages will contain one *.d.ts for each subpackage.

  1. We need to edit scripts/proxy-dirs.js to add a types field to the package.json.
nicolodavis commented 4 years ago

About where to expose base.ts, I think an internal package sounds good.

nicolodavis commented 4 years ago

Try v0.39.2.

Async should be available in boardgame.io/internal. Not every sub-package will have types (client, for example) because we haven't converted the relevant source files to TS.

delucis commented 4 years ago

👍 From a quick check, it looks like it works as expected for packages that have been converted, but not for server. What’s the reason for the server sub-package being provided differently from the other sub-packages?

The other thing that would be handy would be to be able to access the basic types defined in src/types.ts. Maybe the types field in boardgame.io’s top-level package.json could point to ./dist/types/src/types.d.ts and then those could be imported directly from boardgame.io:

import type { State } from 'boardgame.io'

I can look into a setting up a server-internal sub-package. I see now that you’ve already exposed Async in boardgame.io/internal. Perfect.

janKir commented 4 years ago

I cannot find any way to access types and interfaces like PhaseConfig from src/types.ts. Can they also be included? EDIT: Nevermind, I found them under boardgame.io/dist/types/src/types.

delucis commented 4 years ago

@janKir They’re currently there, but hopefully in the next patch they’ll be available from a more obvious path. Types were only added a few hours ago!

janKir commented 4 years ago

Thanks! Yes, I just stumbled across this library earlier today when the latest release did not include any types, yet... :D Keep up the great work!

nicolodavis commented 4 years ago

What’s the reason for the server sub-package being provided differently from the other sub-packages?

A good reason might have existed in the past, but I can't think of a reason why we do it differently now.