The problem is when I am using request
, and trying to call the method that is not exist in the interface, TS actually is not looking in the interface, its looks ... somewhere else, please see what's going on:
componentWillUnmount() { request.off(request['EVENT']['SIGN_IN']); request.off(request['EVENT']['SIGN_OUT']); }
So, it's not looking for off
in the TRequestProps
. It's looking in type '({ httpMethod, resourcePath, queryParams, body, headers, debug, customInstance, isAPI, }: TRequestProps) => Promise<unknown>'.ts(2339)
which is not what I need - I need to find a way to look for props exactly in the correct interface, in my case it's TRequestProps
request:
export const request = initOAuth({ baseURL: getEndpointsURL(), logLevel: "info",});
initOAuth:
export default function <T extends { logLevel: string }>(options: T) { const requestFunction = init({ mstorage, emitter, overseer, }); const oauthRequestFunction = requestFunction(); return oauthRequestFunction;}
init:
export type TOverseer = { start: Function; doCheck: Function;};export type TMethodOpts = { overseer: TOverseer;};export default (methodOptions: TMethodOpts | $TSFixMe) => () => { const requestFunction = initRequest(methodOptions); requestFunction["signIn"] = require("./methods/signIn")(methodOptions); requestFunction["signOut"] = require("./methods/signOut")(methodOptions); requestFunction["saveUserSession"] = require("./methods/saveUserSession")( methodOptions ); return requestFunction;};
initRequest:
export interface TRequestProps { httpMethod: string; resourcePath: string; queryParams: $TSFixMe; debug: boolean; isAPI: boolean; body: $TSFixMe; headers: AxiosRequestHeaders; customInstance: $TSFixMe;}export interface IMStorage { isArray: <T>(a: T) => boolean; push: <T>(path: string, value: string) => T; get: <T>(path: string, defaultValue?: $TSFixMe) => T; set: <T>(path: string, value: $TSFixMe) => T; isEmpty: (path: string) => boolean;}
The initRequest
function I am export using IMStorage
and TRequestProps
interfaces:
export default ({ mstorage }: { mstorage: IMStorage }) => ({ httpMethod, resourcePath, queryParams = {}, body = {}, headers = {}, debug = false, customInstance = null, isAPI = true, }: TRequestProps) => { return new Promise((resolve) => { console.debug( `[${resourcePath}] Is token refreshing: `, !!mstorage.get(IN_MEMORY.REFRESHING_TOKEN) ); if ( resourcePath === URL.TOKEN || !mstorage.get(IN_MEMORY.REFRESHING_TOKEN) ) { console.debug(`[${resourcePath}] DoRequest!`); doRequest(resolve, currentInstance); } else { console.debug(`[${resourcePath}] delayRequest!`); delayRequest(resolve); } }); };