Skip to content
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

Ignore Regions of code in compiler #829

Closed
mbeckenbach opened this issue Oct 6, 2014 · 9 comments
Closed

Ignore Regions of code in compiler #829

mbeckenbach opened this issue Oct 6, 2014 · 9 comments
Labels
Duplicate An existing issue was already created

Comments

@mbeckenbach
Copy link

Sometimes i use minified script snippets like AppInsights (code snippet is generated by the azure portal), that creates a class in windows.whatever.

Typescript compiler then complains about unknown things on window.
Code would work great if the compiler did not complain.

It costs lots of time finding a workarround. So creating something simple like regions of code that the compiler should ignore and just copy would be cool. Could be some easy syntax like he IF DEBUG flags in C#.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 6, 2014

The C# conditional compilation flags does not make the compiler copy the code, it ignores it totally if "Debug" is not defined.

Why not just include these in a js file, that you do not pass to the compiler, and include in with you output, either through concatenation or through including it in the html file of your application?

@danquirk
Copy link
Member

danquirk commented Oct 6, 2014

This is essentially a use case for #449 is it not?

@danquirk danquirk closed this as completed Oct 6, 2014
@danquirk danquirk added the Duplicate An existing issue was already created label Oct 6, 2014
@brianharwell
Copy link

I ran into the same issue with App Insights. My thought was instead of having conditional compilation flags, why not specify a region that TypeScript should ignore? Supposedly valid javascript is valid TypeScript but if you try using the App Insights code in an Angular service that is not the case.

What I would like is a #region directive or something that tells the TypeScript compiler to completely ignore the code.

@mhegazy
Copy link
Contributor

mhegazy commented May 8, 2016

Can u share a sample of the issues u are running into?

@brianharwell
Copy link

Sure, take for example the application insights code...


function initialize(key) {
            appInsights = window.appInsights || function (config) {
                function r(config) {
                    t[config] = function () {
                        var i = arguments;
                        t.queue.push(function () { t[config].apply(t, i) });
                    }
                }

                var t = { config: config }, u = document, e = window, o = 'script', s = u.createElement(o), i, f;
                for (s.src = config.url || '//az416426.vo.msecnd.net/scripts/a/ai.0.js', u.getElementsByTagName(o)[0].parentNode.appendChild(s), t.cookie = u.cookie, t.queue = [], i = ['Event', 'Exception', 'Metric', 'PageView', 'Trace']; i.length;) r('track' + i.pop());
                return r('setAuthenticatedUserContext'), r('clearAuthenticatedUserContext'), config.disableExceptionTracking || (i = 'onerror', r('_' + i), f = e[i], e[i] = function (config, r, u, e, o) {
                    var s = f && f(config, r, u, e, o);
                    return s !== !0 && t['_' + i](config, r, u, e, o), s
                }), t;
            }({
                instrumentationKey: 'my guid'
            });

If you take that code and put it in a .ts file compilation will fail. It would be cool if I could do something like...

#region exclude

some js code

#end region

@mhegazy
Copy link
Contributor

mhegazy commented May 18, 2016

looks like you need to define appInsights on Window, something like:

interface Window { 
    appInsights: any;
}
declare var appInsights: typeof window.appInsights;

@mhegazy
Copy link
Contributor

mhegazy commented May 18, 2016

The other option is to leave your non-ts code in a .js file, and include that in your code using --allowjs.

@brianharwell
Copy link

I want to include this logic in an Angular service. The window line isn't the problem. The issue is with one of the array indexers. Having a way to exclude a part of a TS file because I don't want the TS compiler to check it would be a nice to have.

@mhegazy
Copy link
Contributor

mhegazy commented May 18, 2016

My recommendation here is either 1. use any as a type annotation for parts you do not care about, either for the Window.appInsights or for t, u and e above, and that should stop the errors. or 2. put all non-TS code in a set of a JS helper functions in a .js file, and call into these from your ts file.

I do not think we have plans to support exclude regions or preprocesor directives.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants