Language Service doesn't work with custom generics
See original GitHub issueI’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
:
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;
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.