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

types: preset and pluggable list settings type checking #92

Conversation

ChristianMurphy
Copy link
Member

@ChristianMurphy ChristianMurphy commented Jun 27, 2020

builds on #91

allow pluggable list and preset to check settings on first 20 elements

leverages variadic typing and generics to allow the first 20 plugins to have full settings type checking.
plugins after the first twenty can still be added, but no longer have their settings checked.

BREAKING CHANGE: changes to order of the processor settings generic P, to go first and plugin settings S to go after. Plugins directly using the Plugin or Attacher type will need to add the first generic with the processor settings.
NOTE: If the plugin does not directly use the Plugin or Attacher type it may not need to migrate. For example

// this does NOT need to migrate, it will continue to work
const plugin = () => (settings: Settings) => { /* ... */ }

// this DOES need to migrate, settings would passed to the wrong generic parameter
const plugin: Plugin<[Settings?]> = () => (settings: Settings) => { /* ... */ }

MIGRATION: if this is not used in the plugin, set the first generic to unknown
For example

Plugin<[PluginSettings?]>
// becomes
Plugin<unknown, [PluginSettings?]>

// and

Attacher<[PluginSettings?]>
// becomes
Attacher<unknown, [PluginSettings?]>

if it does depend on this, add the processor options in the first generic, for example RemarkOptions or RehypeOptions could be used.

⚠️ TypeScript 4 is currently in beta, this should not be merged until after there is a stable release.
⚠️ Adopting this change would require migration of some plugins' typing


Additional Discussion:

  1. Is there a way to support an artibrary number of plugins in PluggableList? Pointers on the syntax for that would be welcome!
  2. Should Attacher and Plugin keep their current generic order? This would reduce the need for migration, but would introduce incosistency between generic parameter order for Attacher and Plugin vs PluggableList and Preset

when `use([plugin, setting])` syntax is used, typescript can now warn if the settings object does not match with what the plugin expects.
@ChristianMurphy ChristianMurphy added 🦋 type/enhancement This is great to have 🧑 semver/major This is a change ☂️ area/types This affects typings 🌊 blocked/upstream This cannot progress before something external happens first labels Jun 27, 2020
@ChristianMurphy ChristianMurphy requested review from wooorm, Rokt33r, Murderlon and a team June 27, 2020 20:40
@codecov-commenter

This comment has been minimized.

Pluggable<P, S18>?,
Pluggable<P, S19>?,
Pluggable<P, S20>?,
...Array<Pluggable<P, any[]>>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allows more plugins to be added past 20, but without enhanced settings type checking

@ChristianMurphy ChristianMurphy added the 💬 type/discussion This is a request for comments label Jun 27, 2020
@ChristianMurphy ChristianMurphy force-pushed the types/pluggable-list-checking branch 2 times, most recently from 80456e5 to 3a2c185 Compare June 27, 2020 21:02
leverages variadic typing and generics to allow the first 20 plugins to have full settings type checking.
plugins after the first twenty can still be added, but no longer have their settings checked.

BREAKING CHANGE: changes to order of the processor settings generic `P`, to go first and plugin settings `S` to go after. Plugins directly using the `Plugin` or `Attacher` type will need to add the first generic with the processor settings.
NOTE: If the plugin does not directly use the `Plugin` or `Attacher` type it may not need to migrate. For example
```ts
// this does NOT need to migrate, it will continue to work
const plugin = () => (settings: Settings) => { /* ... */ }

// this DOES need to migrate, settings would passed to the wrong generic parameter
const plugin: Plugin<[Settings?]> = () => (settings: Settings) => { /* ... */ }
```
MIGRATION: if `this` is not used in the plugin, set the first generic to `unknown`
For example
```typescript
Plugin<[PluginSettings?]>
// becomes
Plugin<unknown, [PluginSettings?]>

// and

Attacher<[PluginSettings?]>
// becomes
Attacher<unknown, [PluginSettings?]>
```
if it does depend on `this`, add the processor options in the first generic, for example `RemarkOptions` or `RehypeOptions` could be used.
@ChristianMurphy
Copy link
Member Author

/cc @remcohaszing

@ChristianMurphy ChristianMurphy added ☂️ area/types This affects typings and removed ☂️ area/types This affects typings 🌊 blocked/upstream This cannot progress before something external happens first 💬 type/discussion This is a request for comments 🦋 type/enhancement This is great to have 🧑 semver/major This is a change labels Aug 21, 2020
@ChristianMurphy ChristianMurphy deleted the types/pluggable-list-checking branch August 26, 2020 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☂️ area/types This affects typings
Development

Successfully merging this pull request may close these issues.

None yet

2 participants