【问题标题】:Expected 1-2 arguments, but got 0.ts(2554) file.d.ts(49, 17): An argument for 'options' was not provided预期 1-2 个参数,但得到 0.ts(2554) file.d.ts(49, 17):未提供“选项”的参数
【发布时间】:2022-02-20 13:03:40
【问题描述】:

我制作了一个示例代码,通过点击 typescript 中的按钮下载 word 文件,这里是代码

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);

我在“let doc = new Document();”行出现错误,说“(alias) new Document(options: IPropertiesOptions, fileProperties?: IFileProperties | undefined): Document 导入文件 预期 1-2 个参数,但得到 0.ts(2554) file.d.ts(49, 17): 没有提供 'options' 的参数。”

这是完整的错误。 enter image description here

谁能帮忙解决这个错误

【问题讨论】:

    标签: typescript


    【解决方案1】:

    这个错误几乎就是它所说的。 new Document() 还不够。你需要传递选项,而你没有。这意味着你不能有(),括号之间没有任何内容。需要更多信息,但您没有提供。

    这是来自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,
                        }),
                    ],
                }),
            ],
        }],
    });
    

    【讨论】:

    • 我只是想在按下按钮时下载一个空的word文档,那么我应该传递什么作为参数?我在codeandbox中尝试了这个代码sn-p,它在那里工作,但在vs code中它显示错误。 @kshetline
    • 如果 new Document() 确实有效,但 VSCode 强制执行不允许的类型签名,您可以尝试 new (Document as any)()
    • 谢谢你真的成功了
    猜你喜欢
    • 2022-08-24
    • 2021-06-18
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多