javascript - Erro: *.default não é um construtor
Eu recebo o seguinte erro ao testar algum código JavaScript, transpilado de um arquivo TypeScript.
Aqui está o erro:
Error: _mapAction2.default is not a constructor
Aqui está a linha de código que causou o erro:
var mapAction = new MapAction(MapActionType.POLYGONDRAGGED, []);
Aqui está o arquivo Typescript original map-action.ts:
import { IMapAction} from './imap-action';
import { MapActionType } from './map-action-type.enum';
import {LatLngLiteral} from 'angular2-google-maps/core';
export class MapAction implements IMapAction{
type: MapActionType;
paths: Array<LatLngLiteral>;
constructor(mapActionType: MapActionType, paths: Array<LatLngLiteral>){
this.type = mapActionType;
this.paths = paths;
}
public getType(): MapActionType{
return this.type;
}
public getPaths(): Array<LatLngLiteral>
{
return this.paths;
}
}
Aqui está o arquivo .js transpilado map-action.js:
"use strict";
class MapAction {
constructor(mapActionType, paths) {
this.type = mapActionType;
this.paths = paths;
}
getType() {
return this.type;
}
getPaths() {
return this.paths;
}
}
exports.MapAction = MapAction;
//# sourceMappingURL=map-action.js.map
Resposta #1
Você precisa exportar um valor padrão que ficará assim:
export default class MapAction implements IMapAction {...
E importe como:
import MapAction from './map_action_file';
Alternativamente, se você quiser exportar várias coisas do módulo, você pode fazer algo como:
export class MapAction ...
export class MapSomethng ...
E importe da seguinte forma:
import { MapAction, MapSomething } from './map_action_file';
Resposta #2
Outra solução seria adicionar "esModuleInterop": true, em tsconfig.json.
esModuleInterop permite importações padrão de módulos sem exportação padrão.
Resposta #3
Verifique se seu import está correto. você pode ter esquecido {}, por exemplo. Alterar
import LatLngLiteral from '';
para
import { LatLngLiteral } from '';
