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

Safely access properties after calling a method - TypeScript #1684

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

Safely access properties after calling a method - TypeScript #1684

huzaifahj opened this issue Dec 17, 2022 · 3 comments
Labels
discussion M-T: An issue where more input is needed to reach a decision enhancement M-T: A feature request for new functionality question M-T: User needs support to use the project TypeScript-specific
Milestone

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 TypeScript-specific labels Dec 18, 2022
@seratch seratch added this to the 4.x milestone Dec 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion M-T: An issue where more input is needed to reach a decision enhancement M-T: A feature request for new functionality question M-T: User needs support to use the project TypeScript-specific
Projects
None yet
Development

No branches or pull requests

2 participants