Override window.console with typescript

I had javascript code in the error registration code that defines console.logfor certain browsers where it does not exist (IE doesn't / didn’t determine if debugging tools are open).

if (typeof console == "undefined")
{
    window.console = { log: function (msg) { } };
}

The problem when trying to upgrade js to Typescript is that it window.consoleis defined as an interface type Console, and since I do not specify everything that it (obviously) does not compile.

interface Console {
    info(message?: any, ...optionalParams: any[]): void;
    profile(reportName?: string): void;
    assert(test?: boolean, message?: string, ...optionalParams: any[]): void;
    msIsIndependentlyComposed(element: Element): boolean;
    clear(): void;
    dir(value?: any, ...optionalParams: any[]): void;
    warn(message?: any, ...optionalParams: any[]): void;
    error(message?: any, ...optionalParams: any[]): void;
    log(message?: any, ...optionalParams: any[]): void;
    profileEnd(): void;
}

How can I say to ignore this interface and just let me override window.console.

My best guessing attempt doesn't work

window.console = { log: function (msg) { } } : any;
+4
source share
3 answers

, :

window.console = <any>{ log: function (msg) { } };

:

window.console = <any>{ log: () => { } };
+4

. , Duplicate Identifier. , , :)

window.console =
{
    info: (message?: any, ...optionalParams: any[]) =>
    {
        // ...
    },

    profile: (reportName?: string) =>
    {
        // ...
    },

    assert: (test?: boolean, message?: string, ...optionalParams: any[]) =>
    {
        // ...
    },

    msIsIndependentlyComposed: (element: Element) =>
    {
         return false;
    },

    clear: () =>
    {
        // ...
    },

    dir: (value?: any, ...optionalParams: any[]) =>
    {
        // ...
    },

    warn: (message?: any, ...optionalParams: any[]) =>
    {
        // ...
    },

    error: (message?: any, ...optionalParams: any[]) =>
    {
        // ...
    },

    log: (message?: any, ...optionalParams: any[]) =>
    {
        // ...
    },

    profileEnd: () => 
    {
        // ...
    },

    count: (countTitle?: string) => 
    {
        // ...
    },

    groupEnd: () => 
    {
        // ...
    },

    time: (timerName?: string) => 
    {
        // ...
    },

    timeEnd: (timerName?: string) =>
    {
        // ...
    },

    trace: () => 
    {
        // ...
    },

    group: (groupTitle?: string) => 
    {
        // ...
    },

    dirxml: (value: any) => 
    {
        // ...
    },

    debug: (message?: string, ...optionalParams: any[]) => 
    {
        // ...
    },

    groupCollapsed: (groupTitle?: string) => 
    {
        // ...
    },

    select: (element: Element) => 
    {
        // ...
    },
};

, TypeScript .

var x: any = 
{
    log: (msg) =>
    {
        //...
    }
};
window.console = <Console>x;
+3

, : -/

 var w:any = window;
 w.console = { log: function (msg) { } };
0

Source: https://habr.com/ru/post/1541702/


All Articles