Skip to content

[TypeSript] Definition of input argument response in type TransformResult is too restrictive #891

Description

@stonio

If we try the example given in documentation page Non-standard request / response format in a TypeScript file, we get a TS2339 error:

// Example found in https://github.com/tkirda/jQuery-Autocomplete#non-standard-request--response-format
/// <reference path="../node_modules/devbridge-autocomplete/dist/jquery-plugin.d.ts" />

$('#autocomplete').autocomplete({
    paramName: 'searchString',
    transformResult: function (response ) {
        return {
            suggestions: response.myData.map((item) => ({ // ⚠️TS2339 - Property 'myData' does not exist on type 'string | AutocompleteResponse'. Property 'myData' does not exist on type 'string'.
                value: item.valueField,
                data: item.dataField,
            })),
        };
    },
});

What happened

TypeScript compiler report an error TS2339:

Property 'myData' does not exist on type 'string | AutocompleteResponse'.
Property 'myData' does not exist on type 'string'.

What you expected to happen

TypeScript compiler should accept any type for argument response. Definition of response type in TransformResult is too restrictive.

Versions

  • devbridge-autocomplete: 2.0.4
  • jquery: 3.7.1
  • Browser + OS: all

Autocomplete options

$('#autocomplete').devbridgeAutocomplete({
    paramName: 'searchString',
    transformResult: function (response ) {
        return {
            suggestions: response.myData.map((item) => ({
                value: item.valueField,
                data: item.dataField,
            })),
        };
    },
});

Steps to reproduce

  1. Copy the example given in documentation page Non-standard request / response format in a TypeScript file
  2. Try compiling this TypeScript file
  3. See compiler error

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions