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
// I want to be able to do this.x.narrowToString().stringMethod1()
This is clearly out of scope for TypeScript. The narrowToString() method returns a boolean, and that type does not have a stringMethod1(). TypeScript would need to rewrite the code, which is out of scope.
Other than that it sounds like a duplicate of #40562.
π Search Terms
assert types, type narrowing
β Viability Checklist
β Suggestion
asserts return type of a function/method should also be able to return additional types as part of it.
asserts x is T
also returnsx
(can also be written asx & asserts x is T
)fn(x: string | number): string asserts x is string
π Motivating Example
Use case:
I want the ability to narrow down
this
in a class so that I can mimic some level of "extension methods".Here's a playground link: https://www.typescriptlang.org/play/?#code/MYGwhgzhAECyD2ATApiAPPArgF2gFQD5oBvAKGgugDswAnW+Adz3gGVtaBLKgcwAoAlAC5okCMlrYY2ABacY8uElRoIHbjyJlKO6LWTZMtKtA6Zk5SgF9Slimq69YBmUgCMfWfJEIU6BxoEAiQ2NqTA8FRq0AAe0AC81MiMSn5oVJgAtgBGEtAAPtABvASCpAD05fhyMBCumCCI0ABmYJwgAHSkMR3FPM6y7mUVVQCS0IxgVLjY8NC5otkgyKZziHNeEF09NPRMLOyO-AK96k4uQwKkQA
π» Use Cases
See use-case.
Another use-case is something I came up before (Asserts in constructor) section: #38442
Cannot be done.
For the motivating example, I have to create extension objects which adds extra runtime cost.
The text was updated successfully, but these errors were encountered: