インポートの依存関係があるため、これらのTypeScript
クラスは同じ.tsファイルにあります。最初のものは抽象GenericModel
あるTypeScriptのこの循環依存関係をリファクタリングして、別々のTypeScriptファイルを作成することはできますか?
:誰かが私はコードをリファクタリング助けることができれば、私はので、私は輸入品の循環参照を削除することができ感謝し
export abstract class GenericModel {
nodeClass: string;
id: string = "0";
static fromJson(json: any): GenericModel {
if (json.nodeClass === "Entity") {
return EntityModel.fromJson(json);
}
else if(json.nodeClass === "User") {
return UserModel.fromJson(json);
}
return null;
}
}
他の二つのクラスがEntityModel
(下記参照)されていますそしてUserModel
:
export class EntityModel extends GenericModel {
nodeClass: string = "Entity";
alias: string;
description: string;
constructor(public text: string, public id: string, public uuid: string, alias: string, public rand:string = "") {
//[...]
}
//instance methods
//[...]
public static fromJson(json: any): EntityModel {
var entity = new EntityModel(json.text, json.id, json.uuid, json.alias, json.rand);
entity.description = json.description;
if (json.types) {
for (let type of json.types) {
let objectifiedType: EntityModel = EntityModel.fromJson(type);
entity.types.push(objectifiedType);
}
}
if (json.innerEntities){
for (let innerEntity of json.innerEntities) {
let objectifiedInnerEntity: EntityModel = EntityModel.fromJson(innerEntity);
entity.innerEntities.push(innerEntity);
}
}
return entity;
}
}
私がここでやっている何がnodeClass
に基づいて静的呼び出しfromJson()
の階層を使用してJSONの直列化復元です。
それはGenericModel
は別のファイルにした場合、彼らは別のファイルにあった場合は他の2つは、彼らがGenericModel
をインポートする必要があるだろうが、それはEntityModel
とUserModel
をインポートする必要があるだろうことは明らかです。
GenericModel --- has to import --> EntityModel, UserModel
EntityModel --- has to import --> GenericModel
UserModel --- has to import --> GenericModel
それはクラスが別の.TSファイルであり、今も何をしてコードをリファクタリングする方法があるのだろうか。
ありがとうございました!
ありがとうございます!あなたが提案した工場のような新しいクラスを追加する必要があると感じました!インタフェース/ jsonの先端をありがとう:) Btw '' '型UUID = number;' ''は最後にありますか?明けましておめでとうございます! –
これは単なるエイリアスです。あなたはUIDのようなものがありますが、実際にはそれは単なる技術的なものであり、独自のセマンティクスを持っています。しばしばタイプエイリアスを使用します。これにより、期待どおりにUIDのみを使用できるようになり、誤ってバニラ数を渡すことはありません。 –
私の場合、 '' 'uuid'''は文字列です!それでもやってもいいですか? '' 'UUID = string;' '?ありがとう –