6

Google 클로저 컴파일러에는 "@typedef"태그가 있지만 코드에서 사용할 수 있습니까? (나는 그것이 일 것이다라는 것을 알고 있지만, 눈살을 찌푸리게한다?)Google 클로저의 @typedef 태그 사용

그래서 여기 내 타입

 
/** 
* The plan object's typedef 
* @typedef {Object} 
*/ 
Types.Plan = { 
    "style": "bordersmall", 
    "width": "50%", 
    "height": "40%", 
    "x": "20%", 
    "y": "10%", 
    "clickable": true, 
    "moveable": true 
}; 

입니다 그리고 내 JSDoc 주석의 유형을 사용할 수 있습니다.

이 내 IDE가 전달 된 매개 변수에 따라서 선언 된 객체는 코드에서 임의의 장소에서 사용하지 않는

에 자동 완성 내게 줄 수 있습니다.

 
/** 
* The Instructions class 
* @param {Types.Plan} plan  Plan for position and dimension 
* @param {Manager}  manager  The manager 
* @param {Instructions} parent  This widget's parent's instructions 
* @return {Instructions}    Returns the new instructions object 
*/ 
Instructions = function(plan, manager, parent){ 
    plan. 
} 

이렇게 괜찮습니까? 아니면 더 나은 해결책이 있습니까?

답변

6

괜찮습니다. 또한 컴파일러로 추가 유형 검사를 가능하게하는 기록 유형을 사용할 수 있습니다 :

/** 
* @typedef {{ 
* style: string, 
* width: string, 
* height: string, 
* x: string, 
* y: string, 
* clickable: boolean, 
* moveable: boolean 
* }} 
*/ 
var myType = ... 

http://code.google.com/closure/compiler/docs/js-for-compiler.html#types

+1

또한 조합에 편리합니다 :'/ ** @typdef {string | number | boolean} */var myType;' –

7

@typedef 특정 유형으로 객체를 표시하지 않는 유형을 정의하는 데 사용됩니다. 특정 변수를 특정 유형으로 표시하려면 @type {<type>} 주석을 사용하십시오.

@typedef@type {...} 구조와 함께 사용하기 위해 "짧은 손"유형을 정의하는 데 사용됩니다.

표시가되어 있더라도 클로저 컴파일러에 개체의 속성이 현재 입력되지 않았지만 나중에 나타날 수 있습니다.