1

tengo un error Only a void function can be called with the 'new' keyword. cuando intento declarar una instancia de una función. Busque en google pero este mensaje aparece en varios errores distintos. introducir la descripción de la imagen aquí

Funcion:

export default function Default() {
    let object: objeto;
    let $el: any = {};
    let config: any = {};
    let color: any = {};
    let maskTitle: (text: string, datamap: any[]) => string = function(value) { return value };

    object.render = function(valuesLeyend, dataMap: any[]) {

        let legend = $el.selectAll(".legend")
            .data(valuesLeyend.slice())
            .enter().append("div")
            .attr("class", "legend g-row")
            .style("font-family", "Roboto")
            .style('margin', '2px');
            
        legend.append("div")
                .style("width", "15px")
                .style("height", "15px")
                .style("background", color);

        legend.append("div")
            .style("font-size", config.fontSize+"px")
            .style("padding", "0 10px")
            .style("width", "90%")
            .style("overflow", "hidden")
            .style("text-overflow", "ellipsis")
            .style("white-space", "nowrap")
            .html((d) =>{return maskTitle(d,dataMap)});
        return object;
    }

    object.$el = function(value){
        if (!arguments.length) return $el;
        $el = value;
        return object;
    };

    object.config = function(value){
        if (!arguments.length) return config;
        config = value;
        return object;
    };

    object.color = function(value){
        if (!arguments.length) return color;
        color = value;
        return object;
    };
    
    object.maskTitle = function(value){
        if (!arguments.length) return maskTitle;
        maskTitle = value;
        return object;
    }; 
    return object;
}

interface objeto {
    render: (value, datamap: any[]) => any;
    $el: (value) => any;
    config: (value) => any;
    color: (value) => any;
    maskTitle: (value) => any; 
}

La función tiene que retornar obligatoriamente para que funcione como fue pensada originalmente en JS. Actualmente estoy pasando a typeScript y si quiero respetar tal cual como esta en js no me queda otra que preservar la funcion pero me encontre con este error que parece no admitir returns.

2
  • Lo que pasa es que toda la aplicacion usa function. Lo mismo que me comentas lo hice con 2 archivos y funciono perfecto pero bueno.. estaria cambiando la forma en que esta hecho. Quisiera contemplar una en donde no tenga que cambiarlo.
    – BCBB
    el 6 abr. 2022 a las 15:29
  • 1
    Ademas me pasa que hay archivos muy grandes con muchos metodos y si lo paso a class voy a tener que agregar los "this."
    – BCBB
    el 6 abr. 2022 a las 15:31

1 respuesta 1

1

El error te indica que "solo funciones que no retornan nada pueden llamarse con la palabra clave 'new'".

En tu caso solo deberías cambiar esto:

let legend = new Default;

Por esto:

let legend = Default();
1
  • Perfecto marcos eso mismo era. Gracias.
    – BCBB
    el 6 abr. 2022 a las 15:41

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.