简体   繁体   English

如何扩展打字稿接口?

[英]how to extend typescript interfaces?

So i have interface for the request that has header from IoperationParam and body would be same as patientList that is array of object but it throws error patientId can not assigsigned to IGetSpecialtyQuestionsParam any idea what needs to be corrected here ? 因此,我具有从IoperationParam和body头开始的请求的接口将与作为对象数组的patientList相同,但是会引发错误PatientId无法分配给IGetSpecialtyQuestionsParam ,在这里需要解决什么问题?

main.interface.ts main.interface.ts

    export interface IGetSpecialtyQuestionsParam extends IOperationParam {
        patientList: (ISpecialtyQuestionsEntity)[];
    }
    export interface ISpecialtyQuestionsEntity {
        /**
         * <H3>This maps to ESL patientId<H3>
         */
        patientId: string;
        /**
         * 1982-01-10
         */
        dateOfBirth: string;
        /**
         * Female
         */
        gender: "Male"|"Female";
        /**
         * We can default this;
         * Specialty HBS
         */

        sourceSystem: string;


        rxInfo?: (RxInfoEntity)[] | null;
    }

export interface IOperationParam {
    appName: string;
    appId: string
}

You seem to be confusing classes and interfaces. 您似乎在混淆类和接口。 Interfaces only define the shape of the data, not default values. 接口仅定义数据的形状 ,而不定义默认值。

Your interfaces should be: 您的界面应为:

export interface IOperationParam {
  appName: string;
  appId: string
}

export interface IGetSpecialtyQuestionsParam extends IOperationParam {
  patientList: ISpecialtyQuestionsEntity[];
}

export interface ISpecialtyQuestionsEntity {
  patientId: string;
  dateOfBirth: string;
  gender: string;
  sourceSystem: string;
  rxInfo?: RxInfoEntity[];
}

The error you stated: "...patientId can not assigsigned to IGetSpecialtyQuestionsParam..." 您指出的错误:“ ... PatientId无法分配给IGetSpecialtyQuestionsParam ...”

is happening because IGetSpecialtyQuestionsParam extends IOperationParam which doesn't have the property: patientId . 之所以发生这种情况是因为IGetSpecialtyQuestionsParam扩展了IOperationParam ,该属性没有属性: patientId

So, you need to either have patientId in the IOperationParam interface, or add it as part of the IGetSpecialtyQuestionsParam interface, or extend a common interface... whatever works best for your app. 因此,您需要在IOperationParam接口中具有patientId ,或者将其添加为IGetSpecialtyQuestionsParam接口的一部分,或者扩展公共接口...哪种最适合您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM