You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 textif(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 unknownif(exception&&typeofexception==="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.
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
chaining optional unknown
β Viability Checklist
My suggestion meets these guidelines:
β 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
instead of
π» 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.
The text was updated successfully, but these errors were encountered: