Search code examples
typescriptinterfaceencapsulation

Property '<properyName>' of exported interface has or is using private name '<name>'


I declare an interface:

export interface ApiClientMetodOptions {
    initialFilterSatement?: string;
    fieldsMapping?: {
        [K in keyof P]?: string;
    };
    requestParams?: IRequestParams<P>;
    additionalParams?: {
        [key: string]: unknown;
    };
    additionalHeaders?: {
        [key: string]: string;
    };
    cancelOption?: IRequestCancelOption;
}

but get following error: Property 'fieldsMapping' of exported interface has or is using private name 'P'. I done this for encapsulate the type and using it in some methods. For example:

export interface IApiClient {
    /**
     * Generic function for fetching a list of a certain instances
     * @param fieldsMapping function converting fields of `P`-type object to a query field names
*/
getEntities<P, T>(
    url: string,
    options: {
        instanceToEntity?: (instance: unknown) => T;
    } & ApiClientMetodOptions,
): Promise<T[]>;

I don't know "typescript" very deeply. What I'm doing wrong and how to fix this problem?


Solution

  • You need to add the generic parameter P to your interface definition like this:

    export interface ApiClientMetodOptions<P> {
        initialFilterSatement?: string;
        fieldsMapping?: {
            [K in keyof P]?: string;
        };
        requestParams?: IRequestParams<P>;
        additionalParams?: {
            [key: string]: unknown;
        };
        additionalHeaders?: {
            [key: string]: string;
        };
        cancelOption?: IRequestCancelOption;
    }
    

    And then in your method defintion add the parameter as well:

    export interface IApiClient {
        /**
         * Generic function for fetching a list of a certain instances
         * @param fieldsMapping function converting fields of `P`-type object to a query field names
    */
    getEntities<P, T>(
        url: string,
        options: {
            instanceToEntity?: (instance: unknown) => T;
        } & ApiClientMetodOptions<P>,
    ): Promise<T[]>;