sourcetip

C/C++와 같은 typedef

fileupload 2023. 6. 12. 21:49
반응형

C/C++와 같은 typedef

0.9.0에 대해 약간 업데이트된 Basarats의 우수한 컬렉션 라이브러리를 사용하여 다음과 같은 유형을 생성하고 있습니다.

Dictionary<ControlEventType, 
    Dictionary<number, (sender: IControl, 
                        eventType: ControlEventType, 
                        order: ControlEventOrder, 
                        data: any) => void >>

이제는 사용할 때마다 이 글을 전부 적어야 하는 것이 싫습니다.효과적인 접근 방식 중 하나는 다음과 같습니다.

export class MapEventType2Handler extends C.Dictionary<ControlEventType,
                                                C.Dictionary<number,
                                                (sender: IControl,
                                                 eventType: ControlEventType,
                                                 order: ControlEventOrder,
                                                 data: any) => void >> {}

그러면 다음과 같이 쓸 수 있습니다.

EH2: MapEventType2Handler = new MapEventType2Handler();

다음 대신:

EH: Dictionary<ControlEventType, 
        Dictionary<number, 
        (sender: IControl, 
         eventType: ControlEventType, 
         order: ControlEventOrder, 
         data: any) => void >>;

더 좋은 아이디어가 있는 사람?

저는 다양한 기능 시그니처를 큰 성과 없이 '타이프 디핑'하는 실험도 하고 있습니다.

버전 1.4부터 유형 스크립트는 유형 별칭을 지원합니다(소스, 이 답변 참조).

type MapEventType2Handler = Dictionary<ControlEventType, 
    Dictionary<number, 
    (sender: IControl, 
     eventType: ControlEventType, 
     order: ControlEventOrder, 
     data: any) => void >>;

먼저 친절한 말씀 감사합니다 :).

귀사의 솔루션은 실제로 최적입니다.

응답 유형 스크립트에는 선언 공간이 두 개 있습니다.유형 및 변수.

항목을 형식 선언 공간에 도입하는 유일한 방법은 클래스 또는 인터페이스를 통해 도입하는 것입니다(0.8.x는 모듈을 사용하여 형식을 도입할 수도 있음).0.9.x에서 제거됨)

구현을 그대로 유지하고 인터페이스는 구현에 독립적이므로 인터페이스가 작동하지 않습니다.

변수는 형식 선언 공간에 이름을 삽입하지 않기 때문에 작동하지 않습니다.변수 선언 공간에만 이름을 입력합니다.

예:

class Foo {    
}

// Valid since a class introduces a Type AND and Variable
var bar = Foo; 

// Invalid since var introduces only a variable so bar cannot be used as a type
// Error: Could not find symbol. Since compiler searched the type declaration space 
var baz: bar; 

// Valid for obvious reasons 
var x: Foo; 

언어에 매크로가 있으면 원하는 작업을 수행할 수 있지만 현재로서는 클래스+확장자가 유일한 방법입니다.

언급URL : https://stackoverflow.com/questions/17164078/typedef-just-like-c-c

반응형