Home > Blockchain >  Expected 1-2 arguments, but got 0.ts(2554) file.d.ts(49, 17): An argument for 'options' wa
Expected 1-2 arguments, but got 0.ts(2554) file.d.ts(49, 17): An argument for 'options' wa

Time:02-21

i made a example code to download a word file on click of a button in typescript, here is the code

import React from "react";
import ReactDOM from "react-dom";
import { Document, Packer } from "docx";
import { saveAs } from "file-saver";
function App() {
  function saveDocumentToFile(doc, fileName) {
    const packer = new Packer();
    const mimeType ="application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    packer.toBlob(doc).then(blob => {
    const docblob = blob.slice(0, blob.size, mimeType);
    saveAs(docblob, fileName);
  });
}

function generateWordDocument(event) {
  event.preventDefault();
  let doc = new Document();
  saveDocumentToFile(doc, "New Document.docx");
}

return (
 <div className="App">
 <button onClick={generateWordDocument}>Generate Word Document</button>
 </div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

and i got error on the "line let doc = new Document();" saying "(alias) new Document(options: IPropertiesOptions, fileProperties?: IFileProperties | undefined): Document import Document Expected 1-2 arguments, but got 0.ts(2554) file.d.ts(49, 17): An argument for 'options' was not provided."

this is the full error. enter image description here

can someone help to solve this error

CodePudding user response:

The error pretty much means what it says. new Document() isn't enough. You need to pass options, and you didn't. Meaning you can't have () with nothing there between the parenthesis. More information is needed, and you aren't providing it.

Here's an example from the documentation page:

const doc = new Document({
    sections: [{
        properties: {},
        children: [
            new Paragraph({
                children: [
                    new TextRun("Hello World"),
                    new TextRun({
                        text: "Foo Bar",
                        bold: true,
                    }),
                    new TextRun({
                        text: "\tGithub is the best",
                        bold: true,
                    }),
                ],
            }),
        ],
    }],
});
  • Related