Add a ghost property to `InjectionToken` for generic type parameter type safety
See original GitHub issueWhich @angular/* package(s) are relevant/related to the feature request?
core
Description
Currently InjectionToken
doesn’t use the type parameter and thus typescript does not type check it (it is marked as unused).
// Simplified version
class InjectionToken<T> {
constructor(protected name: string) {}
}
interface Person {
name: string;
}
interface Car {
numOfWheels: number;
}
const a = new InjectionToken<Person>('test');
const b = new InjectionToken<Car>('test');
// No error :(
const c: InjectionToken<Person> = b;
Proposed solution
As the generic type is not used anyway - an unused property can be added to InjectionToken
just for this type check ability with the type T
// Simplified version
class InjectionToken<T> {
typeCheck!: T;
// #typeCheck!: T; // Option 2
// protected typeCheck!: T; // Option 3
constructor(protected name: string) {}
}
interface Person {
name: string;
}
interface Car {
numOfWheels: number;
}
const a = new InjectionToken<Person>('test');
const b = new InjectionToken<Car>('test');
// Error :)
const c: InjectionToken<Person> = b;
Alternatives considered
- Wrap
InjectionToken
with a custom class that does it (possible? recommended?) - Ignore the missing type check 😦
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Resolve generic parameter in Angular Generic Service
I state that what I need is for Angular to provide the constructor parameter of type T for my generic Service. If I...
Read more >Constraints on type parameters - C# Programming Guide
The constraint enables the generic class to use the Employee.Name property. The constraint specifies that all items of type T are guaranteed to ......
Read more >Documentation - Generics
Once we've written the generic identity function, we can call it in one of two ways. The first way is to pass all...
Read more >How To Use Generics in TypeScript
TypeScript fully supports generics as a way to introduce type-safety into components that accept arguments and return values whose type will be ...
Read more >Crossing the Generics Divide
Generics are great, until they aren't, and when they aren't is when you ... type because the incoming parameter has closed to the...
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
It’s not just about there being a major release, we also have to fix all the apps that would’ve been broken by the change. There were higher priority changes that we focused on for v15 instead.
@crisbeto ok… Wasn’t 15 a major release? 🤔