Sortable icon indicating copy to clipboard operation
Sortable copied to clipboard

How to use sortable with typescript?

Open ghost opened this issue 7 years ago • 12 comments

I'm unable to import sortable into my typescript project.

I installed sortablejs as following:

npm install --save-dev sortablejs
npm install --save-dev @types/sortablejs

Then I tried to import it:

import Sortable from "sortablejs";

but it shows an error message:

... has no default export

then I tried:

import * as Sortable from "sortablejs"

and it shows another error:

module sortablejs resolves to a non-module entity and cannot be imported using this construct.

I also cannot use require, because in our company we are not allowed to use it, because it will hinder stuff like tree shaking etc.:

import Sortable = require('sortablejs')

So, how do I supposed to use this library with typescript?

ghost avatar Mar 28 '17 18:03 ghost

Have the same problem..waiting for a response...

PasinduDineth avatar Mar 30 '17 08:03 PasinduDineth

Same here! How can I use it with Typescript?

jgoldhammer avatar Apr 28 '17 19:04 jgoldhammer

https://github.com/SortableJS/angular-sortablejs is Typescript. Maybe helps?

octlabs avatar May 17 '17 20:05 octlabs

I've sent PR to be able to use import, but as suggested in the PR, the compiler option fixes it.

martingold avatar Jul 18 '17 20:07 martingold

This should work:

import Sortable = require('sortablejs');

DerekTBrown avatar Aug 09 '17 06:08 DerekTBrown

Sadly this might be a bit late..

import Sortable from "sortablejs"; 

var sortable = new Sortable(container, options);

jakenuts avatar Sep 30 '18 03:09 jakenuts

problem is still there...

anyone can help out?

it's 2020 and sortable still overwrites the default export instead of exporting like a module should..

Problems right now are:

import Sortable from "sortablejs"

results in: TS1259: Module '".../@types/sortablejs/index"' can only be default-imported using the 'esModuleInterop' flag

import * as Sortable from "sortablejs"

Sortable.create(element);

results in: Sortable.create is not a function

import * as Sortable from "sortablejs"

new Sortable(element);

results in: Sortable is not a constructor

import * as Sortable from "sortablejs"

new Sortable.Sortable(element);

results in: TS2339: Property 'Sortable' does not exist on type 'typeof Sortable'. (although my chrome debugger tells me it does exist)

import { Sortable } from "sortablejs";

results in: TS2617: 'Sortable' can only be imported by using 'import Sortable = require("../../node_modules/@types/sortablejs")' or by turning on the 'esModuleInterop' flag and using a default import. but i don't want to use requirejs... 👎

but sure, lets give it a try:

import Sortable = require("sortablejs");
Sortable.create(element);

Sortable.create is not a function

oke.. so much for typescript support. It simply doesn't work.

BasieP avatar Aug 03 '20 08:08 BasieP

Any updates to this issue please? Can we use sortable with Typescript?

CodenameCookie avatar Oct 12 '20 18:10 CodenameCookie

Did you turn on the esModuleainterop flag in tsconfig.json?

waynevanson avatar Oct 12 '20 21:10 waynevanson

I had the same problem.

The solution for me was to add "allowSyntheticDefaultImports": true in compilerOptions in tsconfig.json. Than just import sortable in ts-file: import Sortable from 'sortablejs';

alexosm avatar Jan 25 '21 17:01 alexosm

as an example:

(function (sortable) {
    new sortable.Sortable(element, {});
})(require('sortablejs'));

ZhukV avatar Jul 22 '21 09:07 ZhukV

Old issue but seems to still be a problem since I run in to it my self too.

npm i --save-dev @types/sortablejs should fix this.

UndercoverNL avatar Feb 17 '22 19:02 UndercoverNL

  1. Install Sortable.js and its type definitions from DefinitelyTyped npm i sortablejs --save npm i @types/sortablejs --save-dev

  2. Add the esModuleInterop flag to the "tsconfig.json" file

{
    "compilerOptions": {
        "esModuleInterop": true
    }
}
  1. Import Sortable in your TypeScript file import Sortable from "sortablejs";

  2. Use Sortable new Sortable(document.getElementById('test'), {});

bassem-mf avatar Sep 24 '22 18:09 bassem-mf