Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/compiler-cli/src/diagnostics/expression_diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ function refinedVariableType(
}
}

// Special case the ngIf directive ( *ngIf="data$ | async as variable" )
const ngIfDirective =
templateElement.directives.find(d => identifierName(d.directive.type) === 'NgIf');
if (ngIfDirective) {
const ngIfBinding = ngIfDirective.inputs.find(i => i.directiveName === 'ngIf');
if (ngIfBinding) {
const bindingType = new AstType(info.members, info.query, {}).getType(ngIfBinding.value);
if (bindingType) {
return bindingType;
}
}
}

// We can't do better, return any
return info.query.getBuiltinType(BuiltinType.Any);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ describe('expression diagnostics', () => {
</div>
`,
'Identifier \'nume\' is not defined'));
it('should accept an async *ngIf', () => accept(`
<div *ngIf="promised_person | async as p">
{{p.name.first}} {{p.name.last}}
</div>
`));
it('should reject misspelled field in async *ngIf', () => reject(
`
<div *ngIf="promised_person | async as p">
{{p.name.first}} {{p.nume.last}}
</div>
`,
'Identifier \'nume\' is not defined'));
Comment thread
andrius-pra marked this conversation as resolved.
Resolved
it('should reject access to potentially undefined field',
() => reject(`<div>{{maybe_person.name.first}}`, 'The expression might be null'));
it('should accept a safe accss to an undefined field',
Expand Down
14 changes: 14 additions & 0 deletions packages/language-service/test/completions_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ describe('completions', () => {
expectContains(fileName, 'name', 'name', 'street');
});

it('should be able to get completions for exported *ngIf variable', () => {
const fileName = mockHost.addCode(`
interface Person {
name: string,
street: string
}

@Component({template: '<div *ngIf="promised_person | async as person">{{person.~{name}name}}</div'})
export class MyComponent {
promised_person: Promise<Person>
}`);
expectContains(fileName, 'name', 'name', 'street');
});

it('should be able to infer the type of a ngForOf with an async pipe', () => {
const fileName = mockHost.addCode(`
interface Person {
Expand Down