I am getting an error at $deferred.promise();
about "'unknown' is not assignable to type 'string'". Can I set this to a string?
Error TS2322 (TS) Type 'JQueryPromise<unknown>' is not assignable to type 'JQueryPromise<string>'.
Type 'unknown' is not assignable to type 'string'.
this.cyclesService
.compileCycle(cycle, allIndications, regimenPart.Cycles)
.done((value) => {
cycle.Sentence.CompiledText = value;
});
public compileCycle(cycle: Models.Regimen.CycleModel, indications: Models.Indications.IndicationModel[], context: Models.Regimen.CycleModel[]): JQueryPromise<string> {
var requestData = { Cycle: cycle, Indications: indications, Context: context };
var $deferred = $.Deferred();
$.ajax({
async: true,
type: 'post',
data: JSON.stringify(requestData),
contentType: 'application/json',
url: this.apiUrls.CompileCycle,
})
.done((response) => {
var value = response.Value;
if (value.length > 0) {
value = value.charAt(0).toUpperCase() value.slice(1);
}
$deferred.resolve(value);
})
.fail(() => {
$deferred.reject();
});
return
$deferred.promise();
}
[HttpPost]
public IHttpActionResult CompileCycle([FromBody]CycleSentenceRequest request)
{
request.Cycle.Sentence.Indications = request.Indications.ToList();
request.Cycle.Sentence.Context = request.Context?.ToList();
request.Cycle.Sentence.SequenceOrder = request.Cycle.SequenceOrder;
return Ok(new { Value = request.Cycle.Sentence.CompiledText, ValueInContext = request.Cycle.Sentence.CompiledTextInContext });
}
CodePudding user response:
I believe the deferred object will have the correct type if it's instantiated like this:
var $deferred = $.Deferred<string>();
// now typeof $deferred.promise() is JQueryPromise<string>
Just by the way, this:
return
$deferred.promise();
essentially means this:
return undefined;
// $deferred.promise(); <- dead code, the method already returned
Please make sure you don't break the line right after the return
statement.