2013-01-15 5 views
1

이벤트에 문제가 있습니다.TypeScript의 정의되지 않은 속성

가 나는 두 개의 인터페이스를 만들었습니다

export interface IEEvent extends JQueryEventObject, MSEventObj { 
    preventDefault:() => void; 
    cancelBubble: bool; 
} 

export interface IEElement extends HTMLElement { 
    click: (event?: IEEvent) => void; 
    onmousedown: (event?: IEEvent) => void; 
    onmousemove: (event?: IEEvent) => void; 
    onmouseup: (event?: IEEvent) => void; 
} 

내가 onmousedown\move\up 소품을 설정하려고 내가 오류가 ...

public static StopPropagation(element: HTMLElement): void { 
    (<IEElement> element).onmousedown = StopPropagationHandler; // error here 
    (<IEElement> element).click = StopPropagationHandler; // error here 
    (<IEElement> element).onmouseup = StopPropagationHandler; // error here 
} 

private static StopPropagationHandler(e?: IEEvent): void { 
    if (typeof (e) === "undefined") { 
     e = <IEEvent> window.event; 
    } 
    if (typeof (e.preventDefault()) !== "undefined") { // error here 
     e.preventDefault(); // error here 
    } 
    e.cancelBubble = true; 
} 

Properties

Method

방법 이 오류를 제거 하시겠습니까?

+1

은 어디에서'StopPropagation' 함수를 호출하는? 나는 당신이 유효한 요소를 전달하지 않으므로 당신의 문제가 실제로 존재한다고 생각한다. – Fenton

답변

1

예외가 발생하면 StopPropagation이 호출 스택 맨 위에있는 것처럼 들립니다. 디버거 내부에서이 요소를 검사하면 요소가 정의되지 않았을 것으로 생각됩니다. 이 경우 코드를 수정하거나 예외가 발생하면 예외를 throw하는 것이 좋습니다. 예 :

수 있도록하려면 정의를 해제 :

public static StopPropagation(element: HTMLElement): void { 
    if (element) { 
     (<IEElement> element).onmousedown = StopPropagationHandler; // error here 
     (<IEElement> element).click = StopPropagationHandler; // error here 
     (<IEElement> element).onmouseup = StopPropagationHandler; // error here 
    } 
} 

허용하려면 정의를 해제 :

public static StopPropagation(element: HTMLElement): void { 
    if (!element) { 
     throw new Error("Null argument exception. An element must be provided."); 

    (<IEElement> element).onmousedown = StopPropagationHandler; // error here 
    (<IEElement> element).click = StopPropagationHandler; // error here 
    (<IEElement> element).onmouseup = StopPropagationHandler; // error here 
}