JqueryDeferred resolve method's return type warning of potential undefined

27 Views Asked by At

I'm trying to get types working for the following service call, but my intellisense warns me that the resolve method returns type lmsControllerConfiguration | undefined. I thought having the resolve be in the .done block will always return return the object I'm hoping for, and if it is undefined it would be caught by the .fail block.

        public getControllerByPk(controllerPk: number): JQueryPromise<lmsControllerConfiguration> {
            let deferred: JQueryDeferred<lmsControllerConfiguration> = $.Deferred();

            let requestUrl: string = this.baseURL + `/getControllerByPk?controllerPk=${controllerPk}`;
            $.ajax({
                url: requestUrl,
                method: "GET",
                contentType: "application/json"
            })
                .done((response: lmsControllerConfiguration) => {
                    deferred.resolve(response);
                })
                .fail((error: string) => {
                    deferred.reject(error);
                });

            return deferred.promise();
        }

This is the method signature.

(method) JQueryDeferred<lmsControllerConfiguration>.resolve(value?: lmsControllerConfiguration | undefined, ...args: any[]): JQueryDeferred<lmsControllerConfiguration>

I've tried changing the types around, using different promise types, and type guarding against undefined before resolving the promise, but it still says the type might be undefined.

0

There are 0 best solutions below