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

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

Open
4 tasks done
huzaifahj opened this issue Dec 17, 2022 · 4 comments
Open
4 tasks done

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

huzaifahj opened this issue Dec 17, 2022 · 4 comments
Labels
area:typescript issues that specifically impact using the package from typescript projects auto-triage-skip enhancement M-T: A feature request for new functionality pkg:web-api applies to `@slack/web-api` question M-T: User needs support to use the project semver:major

Comments

@huzaifahj
Copy link

huzaifahj commented Dec 17, 2022

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

  • bug

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

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

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",

@huzaifahj huzaifahj changed the title TypeScript types which function returns contains many optional properties Safely access properties after calling a method - TypeScript Dec 17, 2022
@seratch seratch added the question M-T: User needs support to use the project label Dec 17, 2022
@seratch
Copy link
Member

seratch commented Dec 17, 2022

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
Copy link
Author

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
Copy link
Member

seratch commented Dec 18, 2022

@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).

@seratch seratch added enhancement M-T: A feature request for new functionality discussion M-T: An issue where more input is needed to reach a decision labels Dec 18, 2022
@filmaj filmaj transferred this issue from slackapi/bolt-js Aug 28, 2024
@filmaj filmaj changed the title Safely access properties after calling a method - TypeScript web-api: type-safe response properties access Aug 28, 2024
@filmaj filmaj added semver:major area:typescript issues that specifically impact using the package from typescript projects pkg:web-api applies to `@slack/web-api` and removed discussion M-T: An issue where more input is needed to reach a decision labels Aug 28, 2024
Copy link

👋 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typescript issues that specifically impact using the package from typescript projects auto-triage-skip enhancement M-T: A feature request for new functionality pkg:web-api applies to `@slack/web-api` question M-T: User needs support to use the project semver:major
Projects
None yet
Development

No branches or pull requests

3 participants