Skip to content

[BUG] Typescript - workbook.getWorksheet() return type is Worksheet, but it may in reality return undefined #2192

Closed
@PChol22

Description

@PChol22

🐛 Bug Report

This bug is typescript-only. The getWorksheet method specifies Worksheet as its return type, but when passing a non-existing sheet name as parameter, it returns in reality undefined.

Lib version: 4.3.0

Why is it an issue

If the undefined possibility isn't specified, then it's easy for developers to forget to account for this possibility when parsing a file coming from a user.

Steps To Reproduce

const wb = new ExcelJS.Workbook();
const ws = wb.getWorksheet('nonExisting');
// type of ws : Worksheet
// actual value of ws : undefined
const row = ws.getRow(1) // throws

The expected behaviour:

the return type should be Worksheet | undefined

Possible solution

// modify the return type in index.d.ts
// line 1746
getWorksheet(indexOrName: number | string): Worksheet | undefined;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions