Repository navigation
Closed
Labels
area: coreIssues related to the framework runtimebreaking changeseffort2: daysfeatureLabel used to distinguish feature request from other issuesfreq3: high
Milestone
Description
This issues supersedes and replaces the proposals in #10510 and #4850.
The problem
Assume the following code example:
@Component({
template: `
<ul>
<li *ngFor="let person of people; let i = index">{{i + 0}}: {{person.last}}, {{person.first}}<li>
<ul>
`
})
class MyExample {
people: Person[] = [];
}
class Person {
first: string;
last: string;
}In Angular 2.1, the generated code for person.first is not type checked because the type of person is generated as any. The type of person should be Person. This would also allow the Angular language service to supply first and last as completions for the expression person. in the interpolations.
Solution
- Change type of
NgForandNgForRowto take a type parameterT. - Change type of
ngForOfto beT[] | Iterable<T>. - Change
$implicitinNgForRowto be of typeT. - Modify the code generation to flow the type from the input bindings to the type parameters.
The modified NgFor and NgForRow would look like:
export class NgForRow<T> {
constructor(public $implicit: T, public index: number, public count: number) {}
...
}
@Directive({selector: '[ngFor][ngForOf]'})
export class NgFor<T> implements DoCheck, OnChanges {
@Input() ngForOf: T[] ;
@Input() ngForTrackBy: TrackByFn;
private _differ: IterableDiffer = null;
constructor(
private _viewContainer: ViewContainerRef, private _template: TemplateRef<NgForRow<T>>,
private _differs: IterableDiffers, private _cdr: ChangeDetectorRef) {}
@Input()
set ngForTemplate(value: TemplateRef<NgForRow<T>>) {
if (value) {
this._template = value;
}
}
...
}Here is now a similar example from the above could be generated that flows the types from the input properties to the type parameters:
// User Provided types
class Person {
first: string;
last:string;
}
class MyComp {
items: string[];
people: Person[];
}
// Types representing relavant parts of the corresponding types in Angular
class AppView<T> {
context: T;
}
interface TemplateRef<T> { create(context: T): ViewRef<T>; contextType: T }
interface ViewRef<T> { context: T }
class NgForRow<T> {
$implicit: T;
index: number;
}
class NgFor<T>{
of: T[];
constructor(template: TemplateRef<NgForRow<T>>) { }
}
/**
* Assume we have template such as this:
* <div *ngFor="let person of people; let index = index">
* {{index}}: {{person.first}}
* </div>
*/
// We can auto generate the following function which captures the
// type relation between `of` property and `template`.
function NgFor_Template<T>(of: T[]): TemplateRef<NgForRow<T>> { return undefined as any; }
function NgFor_T<T>(of: T[]): T { return undefined as any; }
// These global variables are here only to flow the type information.
var _comp: MyComp;
// The template row can be constructed as:
var template = NgFor_Template(_comp && _comp.people);
var row: typeof template.contextType;
class RowView extends AppView<typeof row> {
}
var rowView = new RowView();
// Note the type of person is People
var person = rowView.context.$implicit;
// The correct NgFor can be constructed as:
var p1 = NgFor_T(_comp && _comp.people);
var ngFor = new NgFor<typeof p1>(template);Try it in the TypeScript playground.
Reacted by Suguru Inatomi, Trotyl Yu, Fabian Wiles, PatrickJS, Yaroslav Admin, Jonathon Lee, Masato Sasaki, Lazar Ljubenović, Sean G. Wright, youssefabidi and 14 more
Metadata
Metadata
Assignees
Labels
area: coreIssues related to the framework runtimebreaking changeseffort2: daysfeatureLabel used to distinguish feature request from other issuesfreq3: high
Activity
15 remaining items
feat(common): rename underlying `NgFor` class and add a type parameter (
feat(common): added typed overloaded for `AsyncPipe.transform()` (ang…
feat(common): rename underlying `NgFor` class and add a type parameter (
feat(common): added typed overloaded for `AsyncPipe.transform()` (ang…
seangwright commentedon Oct 25, 2017
@chuckjaz I'm wondering what is the current status of this feature?
I'm using the following versions
In my template I'm using
asyncand a template variable but I lose typing support with the template variable.Here
notesDatahas type any and thereforenotehas type any. If I renamenotesDatatonotes, the compiler is ok but I then lose typing support on(notes | async).It would be great to leverage all this great template syntax but if the typing support isn't there I'm more likely to not use the
asyncpipe and instead manage my subscriptions within my component.My alternative (to keep the template typing support) is to use the
asyncpipe twice and then use.publishLast().refCount()on the observable in my component class.mmatthews06 commentedon Jul 14, 2019
Has there been any movement on this? I understand the benefits of TypeScript, are they really being tossed out the window in half the framework's code? Is there a sort of workaround for this?
simeyla commentedon Aug 10, 2019
@mmatthews06 Looks like this is a similar issue - which I about understand 50%!
PS. I'm trying to do this with
[ngTemplateOutletContext]where I seem to [need a function to coerce the type] :-((https://stackoverflow.com/questions/55458421/ng-template-typed-variable/57438839#57438839).pauldraper commentedon Aug 23, 2019
@mmatthews06 workaround is to either migrate to React or other JSX-based system (which has had excellent TS support for years), or to migrate to Angular Ivy, which looks at the types of special static properties.
amitport commentedon Oct 31, 2019
@pauldraper Angular Ivy still suffers from the same issue
shackijj commentedon Jan 28, 2020
I tested Angular Ivy yesterday. The problem is solved for ngFor and ngForOf.
package.json
JoostK commentedon Mar 14, 2020
Support for strict checking of
ngForis fully working using Ivy, ifstrictTemplates(with at leaststrictInputTypes) is enabled.angular-automatic-lock-bot commentedon Apr 14, 2020
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.