-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Allow declaration declare
within conditionals
#25570
Comments
No its not a duplicate of #819 . Its a different use case. In case of #23602 could be partly same. However, #23602 addresses only creation/declaration of the variable in case the Here, in this use case (conditional declaration), I am requesting declaration of a variable if case 1 and declaration of another variable in case 2. This is for But the second use case of allowing conditional declaration is to allow declaration based on a dependent case - declaration of a variable if case 1 and declaration of another variable in case 2 where case 1 object is not defined.
This is the actual request - presumably. But the optional declaration [ |
i am not sure i understand what you mean by "variable if case 1 and declaration of another variable in case 2." are you looking for dependent types? |
|
I am unsure how easy or difficult it will be to implement this but my thoughts (thinking aloud). My requirement was
Use case:
Use case:
Use case:
OR
OR
Use case:
OR
Use case:
Use case:
Use case:
This is different from #23602 where Use Case in #23602:
AND
Proposed in #23602:
|
Yes, quite close. But I believe issue #21316 does not address some above use cases. Say, I dont have an interface or a type and it is an object presence or availability I am checking for, in the global's object context. Here, lets say
Would the issue #21316 suffice? I believe not. The first two will be dependent on whether the polyfill object was loaded or not - when the browser supports Worker Object (and polyfill is not needed). Note that the polyfill of PseudoWorker is there loaded always. Second, in #21316 a conditional type assignation also does not allow for not declaring a variable at all if it is supposed to be undefined (#23602 Hence the need for proposals (ironically, which I did not notice, my requirement seems to be making two change proposals - let me know if another issue is needed or you can tag it up for two features):
AND
AND
AND
AND
|
Looks like all these use cases boil down to conditional types. let's not get stuck on the syntax or how to declare these variables. This request needs the type system to process and understand dependent types (possibly infer them from declarations/conditional statements). |
I do not think we have a specif issue tracking dependent types at the moment. there has been discussions about them in different issues. if you want to log a new suggestions for dependent types that would be great. |
Yes, I understand. That is something I have not considered in a holistic manner, frankly, while creating the issue. My bad. I was only considering simple conditionals for declaration (whereas dependency types or related values may take different structure altogether) since it was needed for a context of a project. May be you could put your thoughts as well based on requests and your experience. Technically #21316 is a dependency type case; and #25570 's main proposal of conditional block declaration may not really face the same complexity, and allows for more flexibility during creation of the language definition without a lot of use case checks (again thats my view). The only dependency I was considering was presence or global or local definitions within conditions. That could be fairly easy to take forward than a bunch of dependency value or type checks. The reason, why my main request was actually allowing conditional block declaration (which can be done relatively easily than a full supported set of features for dependency types inside .ts). Something like this: Use case #23602
Use case #25570
OR
OR
OR
OR Use case #21316
The ways of implementing (my referred) use case can be done in different ways but other use cases may be left out. However, this does not change the language implementation complexity for dependency types. I can raise a separate issue for this, if you believe you can consider this as a feature for future. But, considering we will implement it, creating a single issue for dependency type and then following all others with duplicates tag for different language functionality implementations may be oppressive than beneficial (like in this issue's original recommendation of I can, for sure, create an issue for conditional dependent type based variable declaration alternative (addressing other use cases) - Just wanted to highlight, the
OR, you can get extensive by adding your own declarative conditions:
I definitely believe dependency of types and values can be difficult to implement in different test cases. But conditional block declaration may not be as complex. What are your thoughts on the same? |
Here is another reason why conditional types #21316 will be different from #25570:
|
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Search Terms
declare and variable declaration (for issues)
Suggestion
Allow declaration inside conditionals like below:
Use Cases
Lets take an example of Web Worker support from browser. If the Web Worker (window.Worker) is not available I will switch to a polyfill that creates a window.PseudoWorker object option. The pseudoworker is always loaded irrespective of browser support.
In my .ts class, I will create the following:
An alternative way to approach the following is:
declare var PseudoWorker: Worker | any;
But the issue with this is, I am loading a polyfill with PseudoWorker all the time in the global context irrespective of the fact that Web Worker is supported or not (window.Worker object present or not). Since the Object is always going to be available there is a clash in the object names used.
This is however different from the #23602 issue by the way that:
An alternate recommendation based on the 23602 issue may be
declare? (var Worker: Worker) : (var PseudoWorker: any);
(updated fromdeclare? var Worker: Worker || var PseudoWorker: any;
)My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: