Вызов JavaScript класса из TypeScript: Uncaught TypeError ... не является конструктором - PullRequest
0 голосов
/ 09 марта 2020

Я пытаюсь использовать простой JavaScript класс из atom-select-list пакета внутри моего пакета TypeScript для Atom ( ссылка ).

Я добавил объявление для этой сторонней зависимости и включил его в свой пакет следующим образом:

/// <reference path="../types/atom-select-list.d.ts"/>
import {SelectListView} from 'atom-select-list'; // copmiled to: `const atom_select_list_1 = require("atom-select-list");`

Однако я получаю эту ошибку

~\atom-indent-detective\dist\selector.js:11
Hide Stack Trace
TypeError: atom_select_list_1.SelectListView is not a constructor
    at Object.selector_show (~\atom-indent-detective\dist\selector.js:11:26)
    at HTMLElement.indentDetectiveChooseIndent (../lib/indent-detective.ts:1:12)
    at CommandRegistry.handleCommandEvent (~\atom-nightly\app-1.46.0-nightly8\resources\app\static\<embedded>:11:349292)
    at CommandRegistry.dispatch (~\atom-nightly\app-1.46.0-nightly8\resources\app\static\<embedded>:11:347767)
    at HTMLSpanElement.view.onclick (~\atom-indent-detective\dist\status.js:24:34)

, который указывает на

indentListView = new SelectListView(...) //  compiled to `indentListView = new atom_select_list_1.SelectListView({...`

Это мой tsconfig. json:

{
  "compilerOptions": {
    //// Linting Options - Uncomment options to get more features (usually more restrictive)
//        "strict": true, // includes all of the following and more
    "strictNullChecks": true,
    //    "forceConsistentCasingInFileNames": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noImplicitAny": true,
//    "noImplicitThis": true,
    "noFallthroughCasesInSwitch": true,
    "allowJs": true,

    //// Compilation options
    "declaration": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "incremental": true,
    "inlineSourceMap": true,
//    "preserveConstEnums": true,
//    "sourceMap": true,
    "preserveSymlinks": true,
    "removeComments": true,


//    "jsx": "react",
//    "jsxFactory": "etch.dom",
    "lib": [
      "ES2018",
      "dom"
    ],
    "target": "ES2018",

    "module": "commonjs",
    "moduleResolution": "node",
//    "noLib": false,
//    "importHelpers": true, // if true you should add tslib to deps
//    "skipLibCheck": false,

    "outDir": "../dist"
  },
  "compileOnSave": true
}

I use babel со следующей конфигурацией в моем пакете. json:

  "babel": {
    "presets": ["@babel/typescript"],
    "plugins": [
      "@babel/proposal-class-properties",
      "@babel/proposal-object-rest-spread",
      "@babel/plugin-transform-runtime"
    ],
    "ignore": ["node_modules", "src/test"]
  }

1 Ответ

0 голосов
/ 10 марта 2020

Я решил проблему с помощью:

import SelectListView from 'atom-select-list' 

или

const SelectListView = require('atom-select-list')
...