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 acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.
Comment
I'm working with TypeScript and have a base class, BaseClass, which uses a generic type IsManual to toggle its behavior. The class has a method manually() to update the _isManually state variable. Another method, callRequest(), changes its behavior based on this _isManually state.
I expect that after calling t.manually().callRequest(), TypeScript would dynamically adjust method availability based on the value of _isManually. Specifically, I want to access methods from either the Request class or its request() method depending on the _isManually flag.
However, this is not happening as expected. How can I make TypeScript recognize the methods conditionally?
Property 'request' does not exist on type 'Request | Promise<Response>'.
This contradicts my expectations based on the _isManually flag, as I expected TypeScript to recognize that callRequest() would return an instance of the Request class and allow me to call its request() method.
I also attempted to use TypeScript's conditional types in the callRequest() method to directly depend on the _isManually property, like so:
I hoped that by doing this, the TypeScript compiler would understand the conditional return types and adjust the method suggestions accordingly.
Outcome:
Regardless of whether I called the manually() method or not, TypeScript did not adjust the available methods based on the _isManually flag. The issue remained unchanged: TypeScript did not recognize the methods I expected to be available.
The text was updated successfully, but these errors were encountered:
The only way this sort of thing would work is with #6223 (or #1213). This isn't Stack Overflow so I don't think anyone here is going to be spending much effort resolving this for you.
Acknowledgement
Comment
I'm working with TypeScript and have a base class,
BaseClass
, which uses a generic typeIsManual
to toggle its behavior. The class has a methodmanually()
to update the_isManually
state variable. Another method,callRequest()
, changes its behavior based on this_isManually
state.Here's the relevant code:
I expect that after calling
t.manually().callRequest()
, TypeScript would dynamically adjust method availability based on the value of_isManually
. Specifically, I want to access methods from either theRequest
class or itsrequest()
method depending on the_isManually
flag.However, this is not happening as expected. How can I make TypeScript recognize the methods conditionally?
You can experiment with the code in this TypeScript Playground link
Further Attempts:
When I tried to chain the methods like this:
I received a TypeScript error:
This contradicts my expectations based on the
_isManually
flag, as I expected TypeScript to recognize thatcallRequest()
would return an instance of theRequest
class and allow me to call itsrequest()
method.I also attempted to use TypeScript's conditional types in the
callRequest()
method to directly depend on the_isManually
property, like so:I hoped that by doing this, the TypeScript compiler would understand the conditional return types and adjust the method suggestions accordingly.
Outcome:
Regardless of whether I called the
manually()
method or not, TypeScript did not adjust the available methods based on the_isManually
flag. The issue remained unchanged: TypeScript did not recognize the methods I expected to be available.The text was updated successfully, but these errors were encountered: