Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update return type for getItem() to indicate it can return null #397

Closed
wants to merge 1 commit into from

Conversation

Dopeyr
Copy link
Contributor

@Dopeyr Dopeyr commented Nov 6, 2024

Update items.getItem return type to indicate it can return null

Description

getItem() can return null when nullIfMissing is set to true, but the return type is currently just Item.

Testing

N/A

@Dopeyr Dopeyr requested a review from a team as a code owner November 6, 2024 12:49
@florian-h05
Copy link
Contributor

This is intentional, as by default null is not returned and instead an error is thrown.
Declaring null as output leads to TypeScript type checking inside the IDE to require checking for null when calling getItem(itemName), which is not intended.

@Dopeyr
Copy link
Contributor Author

Dopeyr commented Nov 6, 2024

This is intentional, as by default null is not returned and instead an error is thrown. Declaring null as output leads to TypeScript type checking inside the IDE to require checking for null when calling getItem(itemName), which is not intended.

Thanks @florian-h05 - I did wonder what the solution to this is as getItem(itemName, true) returns me the wrong type and I'm working round it with getItem(itemName, true) as Item | null in typescript.

I looked into @overload in jsdoc to specify two function signatures (if nullIfMissing is not specified, return Item, if it is specified return Item | null) but I wasn't successful. The types output didn't change. I'm new to jsdoc so I could just be missing something simple there.

Thanks, Andy.

@Dopeyr Dopeyr closed this Nov 6, 2024
@florian-h05
Copy link
Contributor

Checking https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html, it doesn’t seem there is a solution for that, which is unfortunate but can be lived with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants