異なる型の配列を作成することについてはquestion on SOがありますが、複数の複合型の配列を作成したいとします。 typescript documentationは私がレポートの配列を作成したい複数の複合型を持つ配列
If we have a value that has a union type, we can only access members that are common to all types in the union.
を言うように、質問へのリンクでの答えが組合のタイプを使用することをお勧めしますが、私はそれが複合型のために十分ではないと思います。しかし、いくつかの報告書は互いに性質が異なり、いくつかは共通している。下のようにコードを作成すると、コード内のどこかで、サブレポートのさまざまなプロパティにアクセスできなくなる可能性があります。
Typescriptで複合型の配列を作成するには、どのような方法が推奨されますか?
interface Reports extends Array<Report>{}
interface Report {
id: number;
timestamp: number;
type: string;
subreport: SubReportA | SubReportB | SubReportC
}
interface SubReportA {
values: []
}
interface SubReportB {
random_property: number;
other_random_name: number;
}
interface SubReportC{
values: [];
name: string;
}
'SubReportA'などは' SubReport'のサブクラスではないのですか?サブレポート:Subreport'があるだけですか? –
配列の使い方の例を挙げることはできますか? – Rodris
@RodrigoPedrosaはその上にマップし、リスト内のReportコンポーネントの各メンバーをレンダリングします。 – Leahcim