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

Handsontable.plugins.BasePlugin cannot be extended: TS 2507: Base is not a constructor function type #6107

Closed
imageslr opened this issue Jul 14, 2019 · 12 comments

Comments

@imageslr
Copy link

imageslr commented Jul 14, 2019

Description

While trying to create a custom plugin using ts, I found that class Base declared in handsontable.d.ts cannot be extended.

Steps to reproduce

just simple codes:

import Handsontable from 'handsontable';
const { registerPlugin } = Handsontable.plugins

class InternalPluginSkeleton extends Handsontable.plugins.BasePlugin {
  constructor(hot) {
    super(hot)
  }
}

export {InternalPluginSkeleton};

// You need to register your plugin in order to use it within Handsontable.
registerPlugin('internalPluginSkeleton', InternalPluginSkeleton);

and I got an error: Type 'Base' is not a constructor function type.

I believe this is similar with the issue #4326 . I wonder know how can I extend BasePlugin and implement my own plugin using ts, without having to build my own handsontable.full.js. Thanks for helping!

@imageslr
Copy link
Author

After changing 2294 line in handsontable.d.ts:

interface Plugins {
+ BasePlugin: typeof plugins.Base;
- BasePlugin: plugins.Base
}

I solved the problem. But I'm not quite sure what problem this modify will cause.

@AMBudnik
Copy link
Contributor

Hey @imageslr

I've asked about the feedback from our devs, but to check if that solution is the right one we'd need to have a demo where the issue can be replicable. Please share one.

@imageslr
Copy link
Author

Hi, here is my demo, but I'm not sure whether it meets your need.

import Handsontable from "handsontable";

class CustomCellStyle extends Handsontable.plugins.BasePlugin {

}

and the error is ts: 2507 Base is not a constructor function type:
image

As a workaround I'm now using below declaration:

import Handsontable from "handsontable";

class CustomCellStyle extends (Handsontable.plugins.BasePlugin as any) {

}

and the error disappeared.

My handsontable version is"handsontable": "^7.1.0".

Thanks for your checking!

@AMBudnik
Copy link
Contributor

I need something where the issue can be replicable. I was thinking about JSFiddle/CodePen or a .zip - what you'd find easier to share.

@imageslr
Copy link
Author

I have tested it with only two files. The files are uploaded below. Thanks!

package.json

{
  "dependencies": {
    "@handsontable/react": "^3.0.0",
    "handsontable": "^7.1.0"
  }
}

index.tsx

import Handsontable from "handsontable";

/**
 * Execute the command below in shell:
 * 
 * tsc index.tsx --jsx --react
 *
 * and I got an error:



▶ tsc index.tsx --jsx react
index.tsx:10:43 - error TS2507: Type 'Base' is not a constructor function type.

10 export default class CustomPlugin extends Handsontable.plugins.BasePlugin {}
                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 2 errors.


 */
export default class CustomPlugin extends Handsontable.plugins.BasePlugin {}

// ------------------------

/**
 * if I change the above line to the line above, compile success.
 * 


▶ tsc index.tsx --jsx react
../node_modules/handsontable/handsontable.d.ts:893:23 - error TS2583: Cannot find name 'WeakSet'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

893       listenersCount: WeakSet<HTMLElement>;
                          ~~~~~~~


Found 1 error.


 */

// export default class CustomPlugin extends (Handsontable.plugins.BasePlugin as any) {}

tsc version

▶ tsc --version
Version 3.5.3

and the files are uploaded.
zip.zip

@AMBudnik
Copy link
Contributor

Hey @imageslr

have you tried to build the plugin using our skeleton https://github.com/handsontable/handsontable-skeleton/blob/master/plugins/internal/internalPluginSkeleton.js
The file has all the comments that will help you to create a plugin.

@imageslr
Copy link
Author

Yes I'm indeed follow the skeleton,except I'm using typescript and causing this problem. It's not a problem with codes, but maybe with handsontable's type definition file.

@AMBudnik
Copy link
Contributor

This case needs further investigation. I'll ask our developer to check it.

@AMBudnik
Copy link
Contributor

Hey :)

we've just published a PR that fixes the issue.

@imageslr
Copy link
Author

Thanks ever so. Very quick fix😄

@AMBudnik
Copy link
Contributor

You were the one to guide us :)

ps. please do not close it yet. We'll close it with the version update.

@AMBudnik AMBudnik reopened this Jul 24, 2019
pnowak added a commit that referenced this issue Aug 2, 2019
pnowak added a commit that referenced this issue Aug 5, 2019
pnowak added a commit that referenced this issue Aug 7, 2019
@pnowak pnowak added this to the August 2019 milestone Aug 7, 2019
pnowak added a commit that referenced this issue Sep 24, 2019
pnowak added a commit that referenced this issue Oct 7, 2019
pnowak added a commit that referenced this issue Oct 8, 2019
* Add some property to Core and change hot property in BasePlugin #6107
@AMBudnik
Copy link
Contributor

Also released in v 7.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants