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

typeahead: Add '| undefined' to ngbTypeahead typings #3907

Closed
AbakumovAlexandr opened this issue Nov 20, 2020 · 0 comments · Fixed by #4038
Closed

typeahead: Add '| undefined' to ngbTypeahead typings #3907

AbakumovAlexandr opened this issue Nov 20, 2020 · 0 comments · Fixed by #4038

Comments

@AbakumovAlexandr
Copy link

Bug description:

Current declaration in typeahead.d.ts is:

ngbTypeahead: (text: Observable<string>) => Observable<readonly any[]>;

i.e. a function must be provided at all times.

According to this typing, if I'd like to have ngbTypeahead working in some cases and disable it in the others, I have to either provide a noop function or use 2 different inputs in a template switched by *ngIf: one with the [ngbTypeahead] directive and another - without it.

Suppose, I have my own component which wraps an input with the [ngbTypeahead] directive:

<input type="text" [ngbTypeahead]="search"/>

My component must be able to support 2 modes: typeahead-enabled mode and just simple text input mode. Most natural way is to declare an undefune-able @Input like the below:

    @Input()
    public autoCompleteItemsFn: ((text: Observable<string>) => Observable<readonly unknown[]>) | undefined;

(where undefined means 'simple input mode') and bind the [ngbTypeahead] directive to it:

<input type="text" [ngbTypeahead]="autoCompleteItemsFn"/>

Which perfectly worked before even if autoCompleteItemsFn === undefined.

However. I can't do this anymore with strict templates due to incomplete typing.

Could you fix it by changing the typing as follows:

ngbTypeahead: (text: Observable<string>) => Observable<readonly any[]> | undefined;

?

Versions of Angular, ng-bootstrap and Bootstrap:

Angular: 10.2.3

ng-bootstrap: 7.0.0

Bootstrap: 4.5.3

@maxokorokov maxokorokov added this to the 8.0.2 milestone Jan 5, 2021
@maxokorokov maxokorokov modified the milestones: 8.0.2, 9.1.0 Jan 12, 2021
maxokorokov added a commit to maxokorokov/ng-bootstrap that referenced this issue Mar 18, 2021
Improves "search" function typings by using standard rxjs `OperatorFunction` type and making search function optional

```ts
// before
@input() ngbTypeahead: (text: Observable<string>) => Observable<readonly any[]>;

// after
@input() ngbTypeahead: OperatorFunction<string, readonly any[]>| null | undefined;
```

Fixes ng-bootstrap#3907
maxokorokov added a commit that referenced this issue Mar 18, 2021
Improves "search" function typings by using standard rxjs `OperatorFunction` type and making search function optional

```ts
// before
@input() ngbTypeahead: (text: Observable<string>) => Observable<readonly any[]>;

// after
@input() ngbTypeahead: OperatorFunction<string, readonly any[]>| null | undefined;
```

Fixes #3907
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants