![](/img/trans.png)
[英]Argument of type '() => JQuery' is not assignable to parameter of type '() => boolean'
[英]Argument of type '() => JQuery<HTMLElement>' is not assignable to parameter of type
我有用於jQuery自動完成的ts代碼
這是代碼
function load_autocomplete_fields(){
$('.airport_field').each(function() {
$(this).autocomplete({
delay:10,
minLength: 0,
source(request, response) {
$(this.element[0]).attr('data-req-term', request.term);
$.ajax({
url: $(this.element[0]).attr('data-source'),
dataType: "json",
data: {
term: request.term
},
success(data) {
const results = [];
$.map(data.cities, function(value, key) {
results.push(value);
return $.map(value.airports, (value2, key2) => results.push(value2));
});
$.map(data.airports, (value, key) => results.push(value));
return response(results);},
error() { return response([]); }
});
return null;},
focus(event, ui) {
return false;},
select(event, ui) {
const qel = $(event.currentTarget);
qel.val(ui.item.fullname);
$(qel.attr('data-id-element')).val(ui.item.id);
return false;}
}).data("ui-autocomplete")._renderItem = function(ul, item) {
return create_autocomplete_item($(this.element[0]), ul, item);
};
$('.airport_field').on('autocompleteselect', function() {
if (this.id.indexOf('origin') !== -1) {
const id = this.id.split('_')[2];
return $(`#search_legs_${id}_destination_text`).focus();
}
});
return $('.airport_field').focus(function() {
if (!$(this).val()) { return $(this).val(' ').keydown(); }
});
});
}
嘗試編譯時出現此錯誤
類型'()=> JQuery'的參數不能分配給類型'(this:HTMLElement,index:number,element:HTMLElement)=> false | 無效”。 類型'JQuery'不能分配給類型'false | 無效”。 不能將類型“ JQuery”分配給類型“ void”。
在此行$('.airport_field').each(function() {
這是HTML
<ul>
<li>
<div class="header"><%= _('Från') %></div>
<%= text_field_tag 'search[legs][0][origin_text]', @default_lsp[:legs][0][:origin_city][:return],
class: 'field-1 airport_field', placeholder: _('Från'),
"data-source" => '/autocomplete/origin/flight',
"data-id-element" => '#search_legs_0_origin_id' %>
<%= hidden_field_tag 'search[legs][0][origin_id]', @default_lsp[:legs][0][:origin_id] %>
<div class="quick-destinations hide-small"><%= generate_quick_links_html.html_safe %></div>
</li>
<li class="icon"><%= image_tag 'shared/right_left_arrow.png', class: 'right_left_arrow', "data-number" => 0 %></li>
<li>
<div class="header"><%= _('Till') %></div>
<%= text_field_tag 'search[legs][0][destination_text]', @default_lsp[:legs][0][:destination_city][:return],
class: 'field-3 airport_field', placeholder: _('Till'),
"data-source" => '/autocomplete/destination/flight',
"data-id-element" => '#search_legs_0_destination_id' %>
<%= hidden_field_tag 'search[legs][0][destination_id]', @default_lsp[:legs][0][:destination_id] %>
</li>
我該如何解決?
在這一行:
return $('.airport_field').focus(function () {
if (!$(this).val()) { return $(this).val(' ').keydown(); }
});
您為什么要返回該呼叫的結果? 除非您想盡早停止迭代,否則each()
方法不希望您返回任何內容,在這種情況下,您應該返回false
。 這就是錯誤告訴您的內容:
Argument of type '() => JQuery' is not assignable to parameter of type '(this: HTMLElement, index: number, element: HTMLElement) => false | void'. Type 'JQuery' is not assignable to type 'false | void'. Type 'JQuery' is not assignable to type 'void'.
手段
該函數返回一個
JQuery
,不合適。 它應該返回false
或void
,而JQuery
也不是其中之一。 具體來說,它不是void
。
也許您應該將上述行更改為
$('.airport_field').focus(function () {
if (!$(this).val()) { return $(this).val(' ').keydown(); }
});
沒有return
,看看是否可行。
希望能有所幫助。 祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.