slackapi / node-slack-sdk

Slack Developer Kit for Node.js
https://tools.slack.dev/node-slack-sdk/
MIT License
3.28k stars 662 forks source link

web-api: type-safe response properties access #1902

Open huzaifahj opened 1 year ago

huzaifahj commented 1 year ago

What type of issue is this? (place an x in one of the [ ])

Requirements (place an x in each of the [ ])


Bug Report

const slackUser = await app.client.users.info({
    user: installation.user.id,
})

slackUser.user.name // Object `user` is possibly 'undefined'.

If there was an error here, the function would throw an error and we could catch it somewhere else. It's confusing me and should be a bug that we can't safely access properties after a function is called with no errors.

Should we be having to use if {} else {} to safely access these properties?

Reproducible in:

package version: "@slack/bolt": "^3.12.2",

seratch commented 1 year ago

Hi @huzaifahj, thanks for asking the question!

Indeed, some of the properties are not actually optional, but this has been a limitation of the package due to several factors such as the complexity of Slack response data patterns, lack of resources in our SDK maintenance team, and so on.

In TS code, you can safely access the property in the following way:

slackUser.user?.name!;

I hope this was helpful to you.

huzaifahj commented 1 year ago

Thanks @seratch for confirming my suspicions.

I am aware of the ways to bypass the type checking in the way you suggested, but this requires external knowledge that the properties will be included in the response.

I would like to leave this issue open and change it to a feature improvement rather than a bug so that the team can track this issue, if that's okay?

seratch commented 1 year ago

@huzaifahj

I would like to leave this issue open and change it to a feature improvement rather than a bug so that the team can track this issue, if that's okay?

This is fine, but perhaps we won't be able to put efforts on the improvement in the short term (meaning, even if it happens, it can be longer than a few months for sure. And, honestly, it'll be possibly years or never).

github-actions[bot] commented 1 month ago

👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.