Typescript: 'typeof FooBar' μœ ν˜•μ˜ μΈμˆ˜λŠ” 'new () => FooBarInterface' μœ ν˜•μ˜ λ§€κ°œλ³€μˆ˜μ— ν• λ‹Ήν•  수 μ—†μŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2015λ…„ 04μ›” 16일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: microsoft/TypeScript

μ•ˆλ…•ν•˜μ„Έμš” μ—¬λŸ¬λΆ„ μ €λŠ” InversifyJS λΌλŠ” TypeScript μ•±μš© IoC μ»¨ν…Œμ΄λ„ˆλ₯Ό 개발 service type (μΈν„°νŽ˜μ΄μŠ€)와 implementation type (클래슀) κ°„μ˜ 바인딩을 μ •μ˜ν•˜λŠ” 데 μ‚¬μš©λ˜λŠ” TypeBinding<TServiceType> λΌλŠ” 클래슀λ₯Ό κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€. 클래슀 μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

// Defines allowed scope modes
enum TypeBindingScopeEnum {
  Transient,
  Singleton
}

class TypeBinding<TServiceType> {

    // The runtime identifier used because at runtime
    // we don't have interfaces
    public runtimeIdentifier : string;

    // The constructor of a class that must implement TServiceType
    public implementationType : { new(): TServiceType ;};

    // Cache used to allow singleton scope
    public cache : TServiceType;

    // The scope mode to be used
    public scope : TypeBindingScopeEnum;

    constructor(
      runtimeIdentifier : string,
      implementationType : { new(): TServiceType ;},
      scopeType? : TypeBindingScopeEnum) {

      this.runtimeIdentifier = runtimeIdentifier;
      this.implementationType = implementationType;
      this.cache = null;
      if(typeof scopeType === "undefined") {
        // Default scope is Transient
        this.scope = TypeBindingScopeEnum.Transient;
      }
      else {
        if(TypeBindingScopeEnum[scopeType]) {
            this.scope = scopeType;
        }
        else {
          var msg = `Invalid scope type ${scopeType}`;
          throw new Error(msg);
        }
      }
    }
}

TypeBinding<TServiceType> 클래슀λ₯Ό ν…ŒμŠ€νŠΈν•˜κΈ° μœ„ν•΄ λͺ‡ 가지 μ—”ν‹°ν‹°λ₯Ό μ •μ˜ν–ˆμŠ΅λ‹ˆλ‹€.

interface FooInterface {
  logFoo() : void;
}

interface BarInterface {
  logBar() : void;
}

interface FooBarInterface {
  logFooBar() : void;
}

// Notice default constructor
class Foo implements FooInterface {
  public logFoo(){ 
    console.log("foo"); 
  }
}

// Notice default constructor
class Bar implements BarInterface {
  public logBar(){ 
    console.log("bar"); 
  }
}

// Notice dependencies on FooInterface and  BarInterface on constructor
class FooBar implements FooBarInterface {
  public foo : FooInterface;
  public bar : BarInterface;
  public logFooBar(){ 
    console.log("foobar"); 
  }
  constructor(FooInterface : FooInterface, BarInterface : BarInterface) {
    this.foo = FooInterface;
    this.bar = BarInterface;
  }
}

Foo 및 Bar ν΄λž˜μŠ€μ— 문제 없이 TypeBinding<TServiceType> 클래슀λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

var fooBinding = new TypeBinding<FooInterface>("FooInterface", Foo);
var barBinding = new TypeBinding<BarInterface>("BarInterface", Bar);

λ‚΄ λ¬Έμ œλŠ” { new(): TServiceType ;} λ₯Ό μ‚¬μš©ν•˜λ €κ³  ν•˜κ³  TServiceType λ₯Ό κ΅¬ν˜„ν•˜λŠ” 클래슀의 μƒμ„±μžκ°€ λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” 경우 λ°œμƒν•©λ‹ˆλ‹€.

'typeof FooBar' μœ ν˜•μ˜ μΈμˆ˜λŠ” 'new () => FooBarInterface' μœ ν˜•μ˜ λ§€κ°œλ³€μˆ˜μ— ν• λ‹Ήν•  수 μ—†μŠ΅λ‹ˆλ‹€.

var fooBarBinding = new TypeBinding<FooBarInterface>("FooBarInterface", FooBar);

이 문제λ₯Ό μ–΄λ–»κ²Œ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

감사 ν•΄μš” :)

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

class 속성을 λ³€κ²½ν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν–ˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

implementationType : { new(): TServiceType ;},

μ—κ²Œ:

implementationType : { new(...args : any[]): TServiceType ;},

일뢀 λ‹¨μœ„ ν…ŒμŠ€νŠΈ con 확인을 μž‘μ„±ν•©λ‹ˆλ‹€.

감사 ν•΄μš”...

λͺ¨λ“  3 λŒ“κΈ€

class 속성을 λ³€κ²½ν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν–ˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

implementationType : { new(): TServiceType ;},

μ—κ²Œ:

implementationType : { new(...args : any[]): TServiceType ;},

일뢀 λ‹¨μœ„ ν…ŒμŠ€νŠΈ con 확인을 μž‘μ„±ν•©λ‹ˆλ‹€.

감사 ν•΄μš”...

μ˜λ¦¬ν•œ! 이런 정보 κ°μ‚¬ν•©λ‹ˆλ‹€ :)

덕뢄에 ν•¨μˆ˜ λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬λœ μƒμ„±μžμ— λŒ€ν•΄ μœ ν˜• 검사λ₯Ό μˆ˜ν–‰ν•˜λŠ” 방법을 μ°Ύκ³  μžˆμ—ˆμŠ΅λ‹ˆλ‹€. { new(...args : any[]): T ;} ν—ˆμš©ν–ˆμŠ΅λ‹ˆλ‹€.

즉, λ°μ½”λ ˆμ΄ν„° λŒ€μƒμ—μ„œ μœ ν˜• 검사λ₯Ό μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

function decor(oPar: {}): <TFunc extends { new (...args: any[]): A }>(target: TFunc) => void {    
    return function <TFunc extends { new (...args: any[]): A }>(target: TFunc): void {
        console.log(oPar, target);
    }
}

class A { id: string; }
// will error
@decor({})
class B { }

// Works
@decor({})
class C extends A { }

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰

κ΄€λ ¨ 문제

weswigham picture weswigham  Β·  3μ½”λ©˜νŠΈ

siddjain picture siddjain  Β·  3μ½”λ©˜νŠΈ

kyasbal-1994 picture kyasbal-1994  Β·  3μ½”λ©˜νŠΈ

bgrieder picture bgrieder  Β·  3μ½”λ©˜νŠΈ

blendsdk picture blendsdk  Β·  3μ½”λ©˜νŠΈ