caiyexiang / html-docx-js-typescript

Convert HTML documents to docx format.
MIT License
85 stars 18 forks source link

Code from usage example does not compile with typings #18

Open Eric-Arz opened 3 years ago

Eric-Arz commented 3 years ago

When the typings for file-saver '@types/file-saver' are installed the code from your usage does not compile.

import { asBlob } from 'html-docx-js-typescript'
import { saveAs } from 'file-saver'

function saveDocx() {
    asBlob("htmlString").then(data => {
        saveAs(data, 'file.docx') // save as docx file
    }) // asBlob() return Promise<Blob|Buffer>
}

Error Message:

error TS2769: No overload matches this call. Overload 1 of 2, '(data: string | Blob, filename?: string | undefined, options?: FileSaverOptions | undefined): void', gave the following error. Argument of type 'Blob | Buffer' is not assignable to parameter of type 'string | Blob'. Type 'Buffer' is not assignable to type 'string | Blob'. Type 'Buffer' is not assignable to type 'string'. Overload 2 of 2, '(data: string | Blob, filename?: string | undefined, disableAutoBOM?: boolean | undefined): void', gave the following error. Argument of type 'Blob | Buffer' is not assignable to parameter of type 'string | Blob'.

6 saveAs(data, 'file.docx') // save as docx file

Joshua-Flores commented 2 years ago

@Eric-Arz or anyone reading this in the future, the solution is to use the data as a blob. See code example below.

saveAs(data as Blob, 'file.docx)