属性 导出接口的“<properyName>”具有或正在使用私有名称“<name>”

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

我声明一个 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;
}

但出现以下错误: Property 'fieldsMapping' of exported interface has or is using private name 'P'. 我这样做是为了封装类型并在某些方法中使用它。例如:

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[]>;

我不是很了解"typescript"。我做错了什么以及如何解决这个问题?

您需要将通用参数 P 添加到您的接口定义中,如下所示:

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;
}

然后在您的方法定义中也添加参数:

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[]>;