Skip to content

Allow template local variables to publish type information so that IDEs can provide better completion #4850

Closed

Description

@mhevery

Problem

Assume the following code example:

@Component({
  template: `
  <ul>
    <li *ngFor="let user of users; let i = index">{{i + 0}}: {{user.last}}, {{user.first}}<li>
  <ul>
`
})
class MyExample {
  users: List<User> = [];
}

class User {
  first: string;
  last: string;
}

Here the IDE has type information for users, but it does not have type information for user and i. For this reason it can not verify:

  • that i+0 is valid because it does not know that i is a number.
  • that user.last and user.first are valid because it does not know what is the type for user

Solution

Parameterize TemplateRef<T>.

class Row {
  $implicit: any;
  index: number;
  even: boolean;
  odd: boolean;
}

@Directive({
  selector: '[ng-for][ng-for-of]',
})
class NgFor  {
  constructor(templateRef: TemplateRef<Row>, viewContainer:ViewContainer) {
  }  
}

Changing the View API

Currently setting the local variables is done as:

var view: ViewRef;
view.setLocal('$implicit', item)

A better way to do this, which is more consistent with the above proposal is to inject a locals object during view Creation.

var local: Row = new Row(item, index);
var view: ViewRef<Row> = this._viewContainer.createEmbeddedView(templateRef, index, local);

The advantage is that currently ngFor has to compute odd, even regardless of wether it is needed in the template. By injecting a local's object the odd field can be computed.

class Row implements Locals {
  constructor(public $implicit: T, index: number) {}

  get even() => this.index % 2 == 0;
  get odd() => this.index % 2 == 1;
}

This means that the Row object can have a rich API, but unless a field is actually referenced from the template it has no cost of computing these fields. Furthermore by using a typed object we can get better performance on locals then when using hashes.

Add Type to the templates

Requires: read generics from DI dependencies (constructor arguments).

import {User} from '...';

@Component({
  types: {User}
  template: `
  <ul>
    <li *ngFor="let user: User of users; let i = index">{{i + 0}}: {{user.last}}, {{user.first}}<li>
  <ul>
`
})
class MyExample {
  users: List<User> = [];
}

Notice the types in the @Component which sets up a map of template types to real types. This will allow the compiler to generate code such as this.

Generated code:

// Deduce generic Row from NgFor constructor asking for a TemplateRef<Row>
class MyExample_View1<Row> extends AppView<Row> {
  ...
  changeDetection() {
    var user: User /* retrieved from the Template */ = context.$implicit;
    var i = context.index; // Here i is number because context.index is number.
  }

}

class AppView<T> {
  context: T
}

class TemplateRef<T> {
  appView: AppView<T>;
  get context(): T {
    return this.appView.context;
  }
}

Activity

  1. scheglov commented on Oct 21, 2015

    @scheglov
    Contributor

    There are couple of things I don't understand about the proposed solution.

    1. Is every directive declaring local variables going to provide LocalesFactory with a type argument, like Row in case of ng-for?
    2. What is @Input() in: Array<T>;? Currently NgFor declares the ng-for-of property. I expected to see it instead of in.
    3. Is the expectation that IDE will "solve" the set of "type equations" against type parameters using properties in order to know exact types of the Locals subclass fields?
  2. mhevery commented on Oct 21, 2015

    @mhevery
    ContributorAuthor
    1. Yes, a directive would have to implement LocalesFactory to give IDE hints.
    2. Sorry that is a typo, changed to of.
    3. Yes the IDE will have to solve the type equations to propagate the types. Is that a problem? If you have ideas on how to make that better/simpler please share.
  3. scheglov commented on Oct 21, 2015

    @scheglov
    Contributor

    No, it's not a problem.
    I just want to make sure that I correctly understand the solution and how you see it to be used.

    Thank you.
    I think this will give the Angular2 analysis plugin enough type information.

  4. tbosch commented on Oct 23, 2015

    @tbosch
    Contributor

    @scheglov Could you have another look? We just simplified the proposal to not rely on a new interface LocalsFactory but rather to make ViewContainer be parameterized on the locals type. Would this also work?

  5. scheglov commented on Oct 23, 2015

    @scheglov
    Contributor

    Yes, this works too.

  6. modified the milestone: on Oct 27, 2015
  7. 19 remaining items

  8. tbosch commented on Jun 23, 2016

    @tbosch
    Contributor

    The missing part can be done post final as it is not a breaking change.

  9. removed this from the Angular 2 Final milestone on Aug 4, 2016
  10. mhevery commented on Aug 4, 2016

    @mhevery
    ContributorAuthor

    I think #10510 is a better solution to this problem.

  11. added
    area: core
    and removed on Sep 7, 2016
  12. chuckjaz commented on Oct 19, 2016

    @chuckjaz
    Contributor

    Closed in favor of #12398

  13. angular-automatic-lock-bot commented on Sep 10, 2019

    @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.

  14. locked and limited conversation to collaborators on Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: corebreaking changeseffort2: daysfeature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions