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

feat(v2): Provide blog plugin theme typing #3267

Merged
merged 6 commits into from
Aug 17, 2020
Merged

feat(v2): Provide blog plugin theme typing #3267

merged 6 commits into from
Aug 17, 2020

Conversation

SamChou19815
Copy link
Contributor

@SamChou19815 SamChou19815 commented Aug 11, 2020

Motivation

Although our classic-theme is written in TS, readers of the source code still can't easily see what's the prop types of each theme component. This is because we rely on the webpack alias @theme, which makes typing difficult to enforce, even on TS sources.

In the past, the best we can do is to add a catch all @theme/* declaration in https://github.com/facebook/docusaurus/blob/master/packages/docusaurus-module-type-aliases/src/index.d.ts. It makes writing theme code in TS a much better experience because there is no longer red squiggles, but the types are all any, which is not good for catching bugs.

I think it's debatable whether we should provide typing information to all classic theme components, since some of them are considered as implementation detail. However, I think we should at least provide some typing information for some theme components expected by the plugins, since they are part of the public APIs exposed by a plugin and would be relied upon by third-party plugins. I think we should not throw these typing info into @docusaurus/module-type-aliases, because it's designed to be more general purposed. Docusaurus core shouldn't care about the plugin-specific theme components; instead, it's the plugin packages' job to define those.

Therefore, I decided to start with the blog plugin, which has a relatively simple interface. I defined the types in the index.d.ts in the blog plugin and make the classic theme reference it. Then the classic theme can at least import the type definition of Props and use it for better typing. I think it's a good starting point to fully statically type the classic-theme.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

It still compiles, which shows that the typing information is accurate enough for our purposes. Preview works.

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Aug 11, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 6757b48

https://deploy-preview-3267--docusaurus-2.netlify.app

@SamChou19815 SamChou19815 marked this pull request as ready for review August 11, 2020 23:25
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Aug 11, 2020
@slorber
Copy link
Collaborator

slorber commented Aug 17, 2020

Thanks, yes that looks like a good starting point.
I guess we should figure out how to have good typings on our own site to be able to recommend the way to use TS for all.

@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Aug 17, 2020
@slorber slorber merged commit 5fdf96c into facebook:master Aug 17, 2020
@SamChou19815 SamChou19815 deleted the blog-plugin-theme-typing branch August 24, 2020 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants