3

나는 다음과 같은 한 타이프 라이터 코드 :억제 타이프 라이터 경고

private addBrowserNameAsCssClassToHtmlTag(): void { 
    var rootElement = angular.element(document.querySelector("html")); 
    if (this.isOpera()) { 
    rootElement.addClass("opera-browser"); 
    } else if (this.isFirefox()) { 
    rootElement.addClass("firefox-browser"); 
    } else if (this.isSafari()) { 
    rootElement.addClass("safari-browser"); 
    } else if (this.isIE()) { 
    rootElement.addClass("ie-browser"); 
    } else if (this.isChrome()) { 
    rootElement.addClass("chrome-browser"); 
    } 
} 

private isOpera(): boolean { 
    return (!!(<any>window).opr && !!(<any>opr).addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; 
} 

private isFirefox(): boolean { 
    //noinspection TypeScriptUnresolvedVariable 
    return typeof InstallTrigger !== 'undefined'; 
} 

private isSafari(): boolean { 
    return /constructor/i.test(window.HTMLElement) || ((p): boolean => { 
     return p.toString() === "[object SafariRemoteNotification]"; 
    })(!window['safari'] || safari.pushNotification); 
} 

private isIE(): boolean { 
    return /*@[email protected]*/false || !!window.document.documentMode; 
} 

private isChrome(): boolean { 
    return !!window.chrome && !!window.chrome.webstore; 
} 

코드가 잘 작동하지만 문제는 시간이 오류를 컴파일 발생한다는 것입니다 :

error TS2304: Cannot find name 'opr'. 
error TS2339: Property 'opera' does not exist on type 'Window'. 
error TS2304: Cannot find name 'InstallTrigger'. 
error TS2339: Property 'HTMLElement' does not exist on type 'Window'. 
error TS2304: Cannot find name 'safari'. 
error TS2339: Property 'documentMode' does not exist on type 'Document'. 
error TS2339: Property 'chrome' does not exist on type 'Window'. 
error TS2339: Property 'chrome' does not exist on type 'Window'. 

나는 일반 쓴 경우 자바 스크립트 나는 않을 것 어떤 문제가있다. 어떻게이 코드 부분에 관한 컴파일러 경고를 억제 할 수 있습니까? 아니면 내가 모르는 더 나은 해결책이 있을까요?

답변

1

신속하고 더러운 답변을 사용 브래킷 표기법, 그들은 또 다른 옵션은 컴파일러가 "기본".D을 포함 타이프 라이터를 빌드 할 때, 글로벌 인터페이스를 확장하는 TS

window["opera"] 

체크 입력되지 않습니다. ts 파일 인 경우 lib.d.ts이고, es6을 타겟팅하는 경우 lib.es6.d.ts입니다. 글로벌 인터페이스를 확장하려면 새로운 .dts 파일을 작성하십시오. myGlobalExtentions.d.ts 예를 들어 귀하의 빌드에 포함하십시오.

interface Window { 
    opera:string; 
} 

interface Document { 
    documentMode:string; 
} 
+0

는 IMO가 Window' 또는'Document' '의 정의를 수정하는 것은 좋은 생각이 아니다. 이 여분의 멤버는 여기에서 사용되지 않습니다. 브라우저가 어떤 것인지를 결정하기 위해 검사됩니다. 대괄호 표기법의 더러운 트릭이 여기에 더 적합합니다. – Paleo

+0

대괄호 표기법이있는 옵션 1이 가장 좋음을 동의합니다. 사용 가능한 두 가지 옵션을 보여 주기만하면됩니다. –

4

해결책 :

declare const InstallTrigger: any; 

class Abc { 
    private addBrowserNameAsCssClassToHtmlTag(): void { 
     var rootElement = angular.element(document.querySelector("html")); 
     if (this.isOpera()) { 
      rootElement.addClass("opera-browser"); 
     } else if (this.isFirefox()) { 
      rootElement.addClass("firefox-browser"); 
     } else if (this.isSafari()) { 
      rootElement.addClass("safari-browser"); 
     } else if (this.isIE()) { 
      rootElement.addClass("ie-browser"); 
     } else if (this.isChrome()) { 
      rootElement.addClass("chrome-browser"); 
     } 
    } 

    private isOpera(): boolean { 
     return (!!window['opr'] && !!window['opr'].addons) || !!window['opera'] || navigator.userAgent.indexOf(' OPR/') >= 0; 
    } 

    private isFirefox(): boolean { 
     //noinspection TypeScriptUnresolvedVariable 
     return typeof InstallTrigger !== 'undefined'; 
    } 

    private isSafari(): boolean { 
     return /constructor/i.test(window['HTMLElement']) || ((p): boolean => { 
      return p.toString() === "[object SafariRemoteNotification]"; 
     })(!window['safari'] || window['safari'].pushNotification); 
    } 

    private isIE(): boolean { 
     return /*@[email protected]*/false || !!window.document['documentMode']; 
    } 

    private isChrome(): boolean { 
     return !!window['chrome'] && !!window['chrome'].webstore; 
    } 
}