question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add plugins methods to typescript definition

See original GitHub issue

Should plugins’s api methods be added in the core d.ts file? Or they should have their owns?

If they have their own, they should be merge into one when bundling the package?

I’m trying to use this library at another library, which is written with Typescript, and trying to use plugin methods, it’s driving me crazy. Any thoughts?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:41 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
yprestocommented, Feb 21, 2019

I updated PR #418, another plugin type defs with module augmentation.

I think it is reasonable to stick on import dayjs from 'dayjs' singleton (not re-exporting extend()ed instance of dayjs) for convenience, because we (at least I) don’t want to change installed plugins place-by-place.

It is same as Vue’s plugin architecture, and official firebase plugin uses module augmentation to extend type declaration of Vue itself. Looks widely accepted way 😃

2reactions
bengrycommented, Jan 23, 2019

@iamkun option 3 isn’t necessarily a breaking change (though it can be). Consider that extend can still modify the prototype, and also return the new instance. This means that any existing usage will still work, but to get type safety you’ll need to re-export the instance created by the extend call. Regarding multiple plugins that relay on one another - you can chain them together (or even modify the extend method to take in an array of plugins):

import dayjs from 'daysjs';
import AdvancedFormat from 'dayjs/plugin/advancedFormat';
import RelativeTime from 'dayjs/plugin/relativeTime';

const dayJsWithRelativeTimeAndAdvancedFormat = dayjs.extend(AdvancedFormat).extend(RelativeTime); // type is inferred as dayjs.DayJS & AdvancedFormat & RelativeTime;

// or if we change the `extend` to be defined as:
export class DayJS {
  ...
  extend<T extends PluginFunc>(plugin: T): DayJS & T;
  extend<T extends PluginFunc, U extends PluginFunc>(plugin1: T, plugin2: U): DayJS & T & U;
  extend<T extends PluginFunc, U extends PluginFunc, V extends PluginFunc>(plugin1: T, plugin2: U, plugin3: V): DayJS & T & U & V;
}

you can call it as:
const dayJsWithRelativeTimeAndAdvancedFormat = dayjs.extend(AdvancedFormat, RelativeTime); // type is inferred as dayjs.DayJS & AdvancedFormat & RelativeTime;

The latter is similar to how Object.assign is defined and behaves as.

Do note that order is important here, so if AdvancedFormat would depend on RelativeTime being defined, the above would fail (the other way around though would work). Again, somewhat similar to how Object.assign behaves in regards to overriding properties defined in multiple objects.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module: Plugin - TypeScript: Documentation
Some plugins add or modify top-level exports on existing modules. While this is legal in CommonJS and other loaders, ES6 modules are considered...
Read more >
How to Build A Plugin System with TypeScript
Defining a plugin. We'll define our plugins using an abstract class that is implemented by each plugin. We'll require name and version to...
Read more >
javascript-plugin-architecture-with-typescript-definitions - npm
A custom class can be composed of the core Base class, a set of plugins and default options and distributed as new package,...
Read more >
Is there a way to add methods on the fly to a class using ...
As an example of this, if you install a jQuery plugin you'll want to re-define the IJQuery & IJQueryUtil interface to include the...
Read more >
Typescript - Strapi Developer Docs
Existing JavaScript projects can add TypeScript support through a conversion procedure. TypeScript-enabled projects allow developing plugins ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found