1
他のJS開発者と同様に、私は、TypeScriptコンパイラが理解できる静的な方法で物事を書くことについて幾分新しく考えました。私の基本的なユースケースは、RESTエンドポイントにアクセスするためのジェネリッククラスで利用可能なアクションをカスタマイズすることです。私は、コンストラクタの引数に基づいて基本クラスにメソッド/小道具を動的に追加できますが、動的に(わかりやすく)intellisenseを間違ったものにしています。私はちょうどこの間違ったことを考えていることを知っています、そして、私が欲しいものを達成するための別のパラダイムがあります。コンパイラが理解できるように、どのようにして子クラスからベースクラスをカスタマイズできますか?ベースクラスのメソッド/小道具を条件付きで指定する
class Endpoint<T> {
constructor(
methods: Array<'GET' | 'CREATE'>
) {
// Conditionally add methods to the endpoint based on what was passed in
if (methods.includes('GET')) {
this.get = function get(id: string): Promise<T> {
return new Promise<T>(resolve => resolve());
};
}
if (methods.includes('CREATE')) {
this.create = function create(body: T): Promise<T> {
return new Promise<T>(resolve => resolve());
};
}
}
public get?: (id: string) => Promise<T>;
public create?: (body: T) => Promise<T>;
}
class User extends Endpoint<IUser> {
constructor() {
super(['GET']);
}
}
let u = new User();
// u.create will get transpiled away, but still shows up in intellisense
あなたは一種の抽象的な親クラスを実装しようとしていますか?次に、必要な基底クラスを定義し、そこから拡張されたクラスの必要な実装のための追加のインタフェースを提供するのはなぜですか? – Icepickle
私は私が従うかどうかわからない。 'class User extends EndpointはIGettable 'を実装しています。 –
あるいは 'let u =新しいユーザー()をIGettable'としますか?インスタンスの機能の共通部分として 'IGetCreate 'のような拡張インターフェースを定義していますか?それはうまくいくようです。 –