Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'firstName' does not exist on type 'Option'. Property 'firstName' does not exist on type 'string'. #786

Closed
NatashaBlong opened this issue Apr 11, 2023 · 2 comments

Comments

@NatashaBlong
Copy link

NatashaBlong commented Apr 11, 2023

Version

react-bootstrap-typeahead - 6.1.1
I am also using Typescript for my application

Steps to reproduce

import { Typeahead } from 'react-bootstrap-typeahead';

export default function ExampleForm(): ReactElement {

// The example code is straight from the examples in GitHub

   return(
        <Typeahead
          labelKey={option => `${option.firstName} ${option.lastName}`}
          options={[
            {firstName: 'Art', lastName: 'Blakey'},
            {firstName: 'Jimmy', lastName: 'Cobb'},
            {firstName: 'Elvin', lastName: 'Jones'},
            {firstName: 'Max', lastName: 'Roach'},
            {firstName: 'Tony', lastName: 'Williams'},
          ]}
        />
   );
}

Expected Behavior

No Errors

Actual Behavior

.firstName and .lastName are underlined in the labelKey field, with the following errors:

Property 'firstName' does not exist on type 'Option'.
Property 'firstName' does not exist on type 'string'.

Property 'lastName' does not exist on type 'Option'.
Property 'lastName' does not exist on type 'string'.

The strange thing is that when i run the code in localhost, the typeahead displays and functions as expected.
However the above errors do cause compiling errors when testing.

@NatashaBlong
Copy link
Author

The following solves my issue, but I think there should at least be an update to the documentation, if not an update to the source code, if the example is its intended use.

 import { Typeahead } from 'react-bootstrap-typeahead';
 
 interface Person {
  firstName: string,
  lastName: string
}

const people: Person[] = [
  { firstName: 'Art', lastName: 'Blakey' },
  { firstName: 'Jimmy', lastName: 'Cobb' },
  { firstName: 'Elvin', lastName: 'Jones' },
  { firstName: 'Max', lastName: 'Roach' },
  { firstName: 'Tony', lastName: 'Williams' },
];
 
  function resolveOptionKey(option: unknown): string {
    const person = option as Person;
    return `${option.firstName} ${option.lastName}`;
  }

export default function ExampleForm(): ReactElement {

   return(
        <Typeahead
          labelKey={resolveOptionKey}
          options={people}
        />
   );
}

@ericgio
Copy link
Owner

ericgio commented Apr 11, 2023

Hi @NatashaBlong, this looks like a duplicate of #704. It's a typing issue, so the component will still work as you mentioned. The workaround is more or less what you arrived at, which is to cast the type. Happy to accept a PR to the docs that might make this clearer; some of the docs were written before the conversion to TS and need updating.

@ericgio ericgio closed this as completed Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants