Skip to content

Support generics in NgIf context #27235

Closed

Description

@jotatoledo

🚀 feature request

Relevant Package

This feature request is for @angular/common

Description

Currently the NgIfContext is type agnostic, as it has no info about the type of the bounded value.

Describe the solution you'd like

Type awareness when using expressions of the form:

login$: Observable<Login>
<div *ngIf="login$ | async as login">
	{{ login. }}
</div>

Describe alternatives you've considered

#25983 introduced the required changes, but was abandoned due to lack of feedback

Activity

  1. changed the title [-]feat(common): support generics in NgIf context[/-] [+]Support generics in NgIf context[/+] on Nov 23, 2018
  2. added
    area: commonIssues related to APIs in the @angular/common package
    on Nov 26, 2018
  3. added this to the needsTriage milestone on Nov 26, 2018
  4. sod commented on Jan 18, 2019

    @sod
    Contributor

    In the meantime if you want typechecking by angular you can stop using *ngIf="... as x" syntax and use a sideeffect instead. Not very elegant, but at least type safe and checked by AngularCompilerPlugin and ngc.

    as x syntax, value is type any

    export interface MyModel {
        baz: number;
    }
    
    @Component({
        selector: 'foo',
        template: `<div *ngIf="stream$|async as stream">{{ stream.bar }}</div>`
    })
    class FooComponent {
        stream$ = of<MyModel>({baz: 1});
    }
    
    // no error reported, `stream` is `any`

    sideeffect, value is type MyModel

    export interface MyModel {
        baz: number;
    }
    
    @Component({
        selector: 'foo',
        template: `<div *ngIf="stream$|async">{{ stream?.bar }}</div>`
    })
    export class FooComponent {
        stream$ = of<MyModel>({baz: 1}).pipe(tap((value) => this.stream = value));
        stream?: MyModel;
    }
    
    // Error: foo-component.ts.FooComponent.html(9,34): : Property 'bar' does not exist on type 'ProductListView'. Did you mean 'baz'?
  5. added
    featureLabel used to distinguish feature request from other issues
    on Apr 25, 2019
  6. modified the milestones: needsTriage, Backlog on Apr 25, 2019
  7. sod commented on Oct 19, 2019

    @sod
    Contributor

    This may come with angular9. Was this fixed by #33016?

  8. angular-robot commented on Jun 4, 2021

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  9. angular-robot commented on Jun 27, 2021

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  10. alxhub commented on Jul 17, 2021

    @alxhub
    Member

    This has been implemented!

  11. angular-automatic-lock-bot commented on Aug 17, 2021

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  12. locked and limited conversation to collaborators on Aug 17, 2021
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

    area: commonIssues related to APIs in the @angular/common packagefeatureLabel used to distinguish feature request from other issuesfreq1: low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions