0

질문은 기본적으로 일반적인 JavaScript 방식으로 구현 된 higher-order components의 유형 확인을 보장하는 방법입니다. 유형 검사에 의해상위 주문 구성 요소에 대한 유형 유효성 검사

Hoc1 = (superclass) => class extends superclass { ... } 
class A { ... } 
class B extends Hoc1(A) { ... } 

나는이 가장 눈에 띄는 유틸리티의 사용 의미 : TypeScript 또는 flow. 내가 any과 거짓 부정적인 미스로

const AMixin: (superclass) => typeof superclass & IAMixinConstructor = 
    (superclass) => class extends superclass implements IAMixin { 
    aMixedMethod() {} 
    } 

다음은 superclass 생각이 방법을 믹스 인을 썼다면 내가 타이프에서 다음 코드와 온 지금까지

,

interface IAMixin { 
    aMixedMethod(): void 
} 
interface IAMixinConstructor { 
    new(): IAMixin 
} 

const AHoc: <T>(superclass: T) => T & IAMixinConstructor = (superclass) => 
    class extends superclass implements IAMixin { 
    aMixedMethod() {} 
} 

class A { 
    aMethod() {} 
} 
class B extends AHoc(A) { 
    bMethod() {} 
} 

const b = new B(); 

b.aMixedMethod(); // false-positive: incrorrectly reports as missing method 
b.aMethod(); 
b.bMethod(); 
b.cMethod(); // this is correctly caught, though 

cMethod의 호출에 오류가 발생했습니다.

적어도 TypeScript에서는 가능합니다. Object.assign인스턴스의 경우 올바르게 작동합니다.. 하지만 같은 종류의 건설이 필요합니다. 그러나 클래스의 경우입니다.

또는 Ruby classes과 같은 클래스 유형이 필요합니까?

+0

에서 [타이프 핸드북 (HTTPS 참조 : // www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html)을 참조하십시오. –

+0

새로운 TS 기능에 대해 더 쉽게 읽을 수있는 https://blog.mariusschulz.com/2017/05/26/typescript-2-2-mixin-classes를 찾았습니다. – kirilloid

답변

1

것이었다 대신 실제 인스턴스의 클래스 및 리턴 값에서 동일 생성자 함수 형식으로 AHoc 파라미터를 정의되었다 :

interface IAMixin { 
    aMixedMethod(): void 
} 

const AHoc: <T>(superclass: new() => T) => new() => (T & IAMixin) = 
    (superclass) => class extends superclass implements IAMixin { 
     aMixedMethod() { } 
    } 

class A { 
    aMethod() {} 
} 
class B extends AHoc(A) { 
    bMethod() {} 
} 

const b = new B(); 

b.aMixedMethod(); // now good 
b.aMethod(); 
b.bMethod(); 
b.cMethod(); // this is correctly caught 
+0

별칭'type 클래스 = new() => T'를 소개하고 그것을 사용하면 코드가 훨씬 더 읽기 쉽다는 것을 알게되었습니다. – kirilloid

관련 문제