私の目標は、名前が別のインターフェイスで定義されているプロパティを持つタイプインターフェイスを作成することです。異なるTypeの構造体を参照する型インタフェース?
私は、typescriptのモデルスキーマオブジェクトに対して次の定義をしています。
export interface ModelSchema {
idAttribute: string;
name: string;
attributes: {
[attrName: string]:
{ type: 'number', default?: number, readOnly?: boolean} |
{ type: 'string', default?: string, readOnly?: boolean} |
{ type: 'boolean', default?: boolean, readOnly?: boolean} |
{ type: 'date', default?: Date, readOnly?: boolean} |
{ type: 'array', default?: string[] | number[], readOnly?: boolean } |
{ type: 'object', default?: object, readOnly?: boolean}
};
relationships: {
[relName: string]: {
type: RelationshipSchema,
readOnly?: boolean,
},
};
};
ここでの説明の鍵となるのは、relationshipsプロパティです。文字列名( 'children'、 'replies'など)を持つ任意の数のプロパティを持つことができ、それぞれはデータモデル内に多対多の関係を表します。
承認者のための別のコンテナを定義したいと思います。サーバーが「このユーザーはC/R/U/Dにアイテム/リレーションシップを許可していますか?それぞれの関係には異なる規則があるため、構造は並列です。各関係には独自の承認者メソッドがあります。
だから、理想的に、私は
export interface ApproverDefinition<S extends ModelSchema> {
typeName: string,
attributes: AttributesAuthorize,
relationships: {
[name: string]: RelationshipAuthorize,
}
}
ような何かを言うだろうが、それは基本的に言うところが別々のガードを持っている:「ApproverDefinition.relationshipsの文字列インデックス付きプロパティと同じ名前を持っている必要がありますこれは、一般的に関係しているとSの関係プロパティのものと、ModelSchema(ここでは擬似コード)のような
何か:。
export interface ApproverDefinition<S extends ModelSchema> {
typeName: string,
attributes: AttributesAuthorize,
relationships: {
[name in keyof S.relationships]: RelationshipAuthorize,
}
}
上記はほぼ完璧です(但し、完全な範囲は必要ありませんが、私はそれと一緒に暮らします) - 私は何をしたいのかを説明しているように感じますが、私はTSから2つのエラーを受け取ります。最初に、私はSの名前をインターフェイスにエクスポートしています。そして、2番目はSを名前空間のように扱っています。
承認者コードとスキーマコードを区別しておく必要がある理由はたくさんあるため、ModelSchemaを拡張するだけでは実際には実現できません。
Object.keys(approver.relationships)とObject.keys(model.relationships)が同じエントリを持っていることを基本的にチェックすると、実行時の型チェックができますが、これをキャプチャしたいのですがタイプスクリプト言語で書かれています。
このようなことはすべて可能ですか?
編集:ここでは例のモデルスキーマです:
const ProfileSchema = {
idAttribute: 'id',
name: 'profiles',
attributes: {
id: { type: 'number', readOnly: true },
short_text: { type: 'string', readOnly: false },
long_text: { type: 'string', readOnly: true },
// SNIP ...
},
relationships: {
memberships: { type: Memberships, readOnly: false },
conversations: { type: ProfilePermissions, readOnly: false },
followingProfiles: { type: FollowingProfiles, readOnly: false},
followingDocuments: { type: FollowingDocuments, readOnly: false},
followingCommunities: { type: FollowingCommunities, readOnly: false},
followers: { type: FollowingProfiles, readOnly: false},
},
};
と私はProfileApprover.relationshipsがProfileSchema.relationshipsと同じ性質を持っていることを
const ProfileApprover: ApproverDefinition<ProfileSchema> = {
typeName: 'profiles'
attributes: /* attribute approver */
relationships: {
memberships: /* approver function */,
conversations: /* approver function */,
followingProfiles: /* approver function */,
followingDocuments: /* approver function */,
followingCommunities: /* approver function */,
followers: /* approver function */,
}
}
ノートを定義したいのですが、プロパティ異なる値を持つ。これは、すべての承認者インスタンスが対応するスキーマと一致するタイプルールで指定したいものです。
承認者が登録されていない場合は、ランタイムエラーが発生することがありますが、これは静的にtypescriptを定義できるようなものです。
上記のスキーマのモックjsonデータを共有できますか? – Aravind
確かにそこにあります。 – pfooti