型付き関数を使用して混合配列から特定の型を取り出すことは可能ですか?typescript - 型付き関数を使用して混合配列から特定のクラスインスタンスを返します
public plugins: (Tool|Service)[] = [];
getTools(): Tool[]{
return this.plugins.filter(t => t instanceof Tool);
}
TS2322以下投げている:タイプ '(ツール|サービス)[]' 'ツール[]' を型に代入できません。タイプ 'サービス'に 'onclick'プロパティがありません。
機能タイプをTool[]
に設定する方法はありますか。ここで
は完全なコードです:
interface Required {
id: string
title: string
owner: string
type: 'user' | 'admin'
}
class P {
id; title; owner; type;
constructor(config: Required){
this.id = config.id || 'uniqid';
this.title = config.title || 'Title';
this.owner = config.owner || 'user';
this.type = config.type;
}
}
interface ToolRequired extends Required{
onclick:() => void
}
class Tool extends P {
onclick;
constructor(config = {} as ToolRequired){
super(config);
this.type = 'tool';
this.onclick = config.onclick
}
}
class Service extends P {
constructor(config = {} as Required){
super(config);
this.type = 'service'
}
}
class Storag {
static types = {
tool: Tool,
service: Service,
undefined: Tool,
};
public plugins: (Tool|Service)[] = [];
setPlugin(config = {} as Required){
const Type = Storag.types[config.type];
this.plugins.push(new Type(config));
}
getTools(): Tool[]{
return this.plugins.filter(t => t instanceof Tool);
}
}
はtypescriptを持っていません2.4これを許可しました - 推論ターゲットとして型を返します(https://blogs.msdn.microsoft.com/typescript/2017/06/27/announcing-typescript-2-4/を参照) –
' Array.map'は間違いなく動作します: 'Array.filter'は次のようなことはしません:c(つまり、' plugins.map(a => a as a Tool) 'vs' plugins.filter(a => a instanceof Tool) ) –
はい - あなたは正しいです。今すぐこれを試してみてください。 –