Skip to content

Commit a87142c

Browse files
committed
fix: type null is not assignable to type IContent
1 parent daeee56 commit a87142c

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/main-library/src/__tests__/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,25 @@ describe("json-as-xlsx", () => {
182182
expect(booksSheet.A3.v).toBe("Git")
183183
expect(booksSheet.B3.v).toBe("Robert")
184184
})
185+
186+
it("should allow null value in content", () => {
187+
const sheets = [
188+
{
189+
sheet: "Users",
190+
columns: [{ label: "IP", value: "metadata.ip" }],
191+
content: [
192+
{ name: "Martin", metadata: { ip: null } },
193+
{ name: "Robert", metadata: { ip: null } },
194+
],
195+
},
196+
]
197+
const buffer = jsonxlsx(sheets, settings)
198+
const workBook = readBufferWorkBook(buffer)
199+
const workSheet = workBook.Sheets.Users
200+
201+
expect(workSheet.A1.v).toBe("IP")
202+
expect(workSheet.A2.v).toBe("")
203+
expect(workSheet.A3.v).toBe("")
204+
})
185205
})
186206
})

packages/main-library/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { utils, WorkBook, WorkSheet, write, writeFile, WritingOptions } from "xl
22

33
export interface IColumn {
44
label: string
5-
value: string | ((value: IContent) => string | number | boolean | Date | IContent)
5+
value: string | ((value: IContent) => string | number | boolean | Date | IContent | null)
66
format?: string
77
}
88

@@ -25,7 +25,7 @@ export interface ISettings {
2525
}
2626

2727
export interface IJsonSheetRow {
28-
[key: string]: string | number | boolean | Date | IContent
28+
[key: string]: string | number | boolean | Date | IContent | null
2929
}
3030

3131
export interface IWorksheetColumnWidth {
@@ -44,7 +44,7 @@ export const getContentProperty = (content: IContent, property: string): string
4444
return value ?? ""
4545
}
4646

47-
if (value === undefined || typeof value === "string" || typeof value === "boolean" || typeof value === "number" || value instanceof Date) {
47+
if (value === undefined || value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" || value instanceof Date) {
4848
return ""
4949
}
5050

@@ -213,4 +213,4 @@ module.exports = xlsx
213213
module.exports.getContentProperty = getContentProperty
214214
module.exports.getJsonSheetRow = getJsonSheetRow
215215
module.exports.getWorksheetColumnWidths = getWorksheetColumnWidths
216-
module.exports.utils = utils
216+
module.exports.utils = utils

0 commit comments

Comments
 (0)