question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typechecking of context doesn't work with custom directives

See original GitHub issue

I’m not sure if this is appropriate but this is basically just a duplicate of https://github.com/angular/vscode-ng-language-service/issues/650

Discussion within that issue seemed to suggest that the issue would be resolved using the ivy language service, however, I cant seem to get that to work and I’m just wondering about the status of the work toward resolving that issue or if there are additional steps I need to take to get it working. I’ve tried using the experimental ivy setting and leaving that option unchecked with no success.

In my scenario, the context is just

export class TemplateContext<T, U extends T[] = T[]> {
  constructor(public $implicit: T, public ref: U){}
}

@Directive({
  selector: '[spTemplateContent]'
})
export class TemplateContentDirective<T, U extends T[] = T[]> {

  @Input()
  spTemplateContentFrom: U

  constructor(public content: TemplateRef<TemplateContext<T>>) {
  }
  static ngTemplateContextGuard<T, U extends T[] = T[]>(dir: TemplateContentDirective<T, U>, ctx: any):
      ctx is TemplateContext<T, U> {
      return true;
    }
}

and using

<... *myDirective="let ctx; from: contentList" ...>
    {{ctx}}
</...>

shows the type of “ctx” to be “any” but I would like it to show the type of an element of “contentList.” For instance, if contentList is a “MyCoolClass[]” then I would like ctx to show as type “MyCoolClass”

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
majellin24commented, Mar 1, 2021

@atscott sorry to keep commenting but actually just setting "strictInputTypes": true solved both issues for me (#650 and #1155) so I wanted to let you know. Thanks a bunch!

1reaction
atscottcommented, Mar 1, 2021

@majellin24 Yes, with the Ivy Language Service enabled, #650 will work. The fix for #1155 is to have either "strictTemplates": true in the angularCompilerOptions or both "fullTemplateTypeCheck": true and "strictInputTypes": true.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typeching of context doesn't work with custom directives #650
Typechecking of context doesn't work. I have copied the ngIf directive and rename it to myIf to test it: Code image; with ngIf...
Read more >
Directive Type Checking - ITNEXT
Unfortunately, custom directives are not completely typed out of the box. The purpose of the article is to show you how to add...
Read more >
Angular structural directive context template type checking
There is no problem with the directive. The problem is that the angular language service is not updated yet to infer those types...
Read more >
Template type checking - Angular
Infers template context types where configured (for example, allowing correct type-checking of NgFor ); Infers the correct type of $event in component/directive ......
Read more >
Typing the Context Object in Angular Structural Directives
In this article, we'll learn how we can type the context object when creating custom structural directives. Let's create a structural ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found