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.

Language Service doesn't work with custom generics

See original GitHub issue

I’m using a new Angular 12 project that was generated with the CLI with the defaults strict modes.

Consider the following component:


@Directive({
  selector: '[appFoo]'
})
export class FooDirective<T extends any[]> {
  @Input() appFoo!: T;
}

This will work as expected and infer the correct type for T: image

But if we change it to a custom type, it will infer it as any:

@Directive({
  selector: '[appFoo]'
})
export class FooDirective<T extends { id: number }> {
  @Input() appFoo!: T;
}

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Jun 21, 2021

Okay, most of these limitations have been resolved in #42492 which just landed for the next release. Any usage of ambient types within a generic type constraint will still not work (using types like Partial, Array, Pick, Readonly, etc) as the compiler is unaware that they don’t need an import. This can now be mitigated by adding an exported type-alias for the type, at which point the compiler is able to use that type-alias.

I will go ahead and close this, as the reported issue and most of the limitations have been addressed.

1reaction
JoostKcommented, May 18, 2021

So the reason it’s limited is that this used to be done for performance reasons only. The actual compiler has a fallback scenario if a generic type is not supported, but the language service cannot use this fallback due to how source files are managed in a live editing context. However, that means that the kinds of generic types we support now directly affect the editing experience in the language service, whereas it used to be invisible.

So yes this is something we want to fix but it’s not a top priority at the moment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with using a defined TypeScript Generic Type in an ...
I'm starting to learn Generics and I find it very powerfull but still have some ways to learn them. Thanks for any help...
Read more >
Zig-style generics are not well-suited for most languages
Zig is an attempt to merge metaprogramming and type system into a single, coherent unit, however not without drawbacks.
Read more >
An Introduction to Generics in Go | by Na'aman Hirschfeld
This function doesn't support custom derived-types, for example the following will panic: type MyInt intvar x MyInt = 1 var doubledX = Double(x) ......
Read more >
How To Use Generics in TypeScript - DigitalOcean
In this code, result has the custom type ProgrammingLanguage because it is passed in directly to the identity function. If you did not...
Read more >
Generics in Go – everything you should know before you start
However, there was no capability to create custom generic types and functions ... For years this programming language did not have generics.
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