Skip to content

unknown: optional chaining support #46785

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

Closed
5 tasks done
mfulton26 opened this issue Nov 11, 2021 · 3 comments
Closed
5 tasks done

unknown: optional chaining support #46785

mfulton26 opened this issue Nov 11, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@mfulton26
Copy link
Contributor

Suggestion

πŸ” Search Terms

chaining optional unknown

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Especially when catching an exception in try…catch but also in other places where the exact type may not matter it would be nice to be able to use some functions for a variable of type unknown with optional chaining.

πŸ“ƒ Motivating Example

try {
  // some code here that may throw an exception
} catch (exception) { // could be anything so it is unknown
   // we might decide we don't care what type exception is only if it can be
  // transformed to a string and if the string includes some text
  if (exception?.toString().includes("HTTP 404")) {
    console.log("uh oh");
  }
}

instead of

try {
  // some code here that may throw an exception
} catch (exception) { // could be anything so it is unknown
  if (exception && typeof exception === "object" && exception.toString().includes("HTTP 404")) {
    console.log("uh oh");
  }
}

πŸ’» Use Cases

I want to avoid writing extra type guards when not really needed so that I'm writing the same amount of code in TypeScript as I would if I were writing it directly in JavaScript.

@andrewbranch
Copy link
Member

Duplicate of #37700

@andrewbranch andrewbranch marked this as a duplicate of #37700 Nov 11, 2021
@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 11, 2021
@mfulton26
Copy link
Contributor Author

I forgot to search among closed issues; my apologies and thank you

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants