2017-06-14 16 views
2

におけるタイプの種類は、ここで有効なtypescriptですスニペット何されていますは活字体

class A{ 
} 

// what is the type of x ? i.e. f(x: <TYPE HERE>)... 
function f(x) { 
    return new x(); 
} 

const t = f(A); 

xの型は、コンストラクタ関数のタイプですが、どのように1は、だろう、それは私には明確でないことは明らかですTypescriptで指定します。

パラメータxを入力することはできますか?

「はい」の場合、そのタイプは何ですか?

を使用でき

答えて

2

interface Newable<T> { 
    new(): T; 
} 

を次のように:

class A {} 

interface Newable<T> { 
    new(): T; 
} 

function f(x: Newable<A>) { 
    return new x(); 
} 

const t = f(A); 

をあなたがそれらを入力する必要がありますコンストラクタ引数を許可する場合:

class A { 
    constructor(someArg1: string, someArg2: number) { 
     // ... 
    } 
} 

interface Newable<T> { 
    new (someArg1: string, someArg2: number): T; 
} 

function f(x: Newable<A>) { 
    return new x("someVal", 5); 
} 

const t = f(A); 

そして、あなた可能性も必要に応じてより汎​​用的なものを実行してください:

interface Newable<T> { 
    new (...args: any[]): T; 
}