2017-10-01 6 views
0

既存のNode.jsクラスに機能を追加する必要があります。具体的にはServerです。次のスニペットの行に沿って何かを使用することは可能ですか?Node.jsクラスを補強する方法は?

import * as net from 'net' 

type ShutdownableServer = net.Server & { shutdown(): Promise<void> } 

function withShutdown(server: net.Server): ShutdownableServer { 
    /* ... */ 
} 

答えて

0

それは、次のスニペットの線に沿って何かを使用することは可能ですか?

はい。

type ShutdownableServer = net.Server & { shutdown(): Promise<void> } 

function withShutdown(server: net.Server): ShutdownableServer { 
    const result = server as ShutdownableServer; 
    result.shutdown = /*your code*/; 
    return result; 
} 

もっと

私はアサーションを使用。ドキュメントhttps://basarat.gitbooks.io/typescript/docs/types/type-assertion.html

関連する問題