ts-plugin-helper contains few generic types that can help you with ScandiPWA plugins (extension) creation.
Repo: https://github.com/Andrii-Antoniuk/ts-plugin-helper
Package: https://www.npmjs.com/package/ts-plugin-helper
Firstly, install the ts-plugin-helper for your extension.
- With npm
npm i ts-plugin-helper
- With yarn
yarn add ts-plugin-helper
- Class properties
// The code below will automatically infer is it member-function (method on class) or member-property
type GetTypesFromMember<
ClassToWhichYouPlugTo,
'nameOfMethodOrPropertyYouPlugTo',
// You can pass new return type if your extension requires that.
// Note, that new return type won't be inferred automatically for other plugins
// It is return type of your **plugin** function
newReturnType = ReturnType<typeof ClassToWhichYouPlugTo["nameOfMethodOrPropertyYouPlugTo"]>>
In case you want to be more precise, you can use:
type GetTypesFromMemberF // for member-function
type GetTypesFromMemberP // for member-property
- Function plugins
// The code below will type safe your plugin function
type GetTypesFromFunction<
typeof functionYouPlugTo,
ContextOfFunction = unknown,
// You can pass new return type if your extension requires that.
// Note, that new return type won't be inferred automatically for other plugins
// It is return type of your **plugin** function
newReturnType = ReturnType<typeof functionYouPlugTo>
>
import CompareIconComponent from '@scandipwa/scandipwa/src/component/CompareIcon';
import { GetTypesFromMember } from 'ts-plugin-helper';
/** @namespace DisableCompare/Plugin/RemoveIcons/renderNoIcon */
const renderNoIcon: GetTypesFromMember<CompareIconComponent, 'render'> = (
args // automatically inferred as [],
callback // automatically inferred as callback function,
_instance // has all of the property as instance class,
) => {
callback(...args);
return null;
};