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.

Add a ghost property to `InjectionToken` for generic type parameter type safety

See original GitHub issue

Which @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

  1. Wrap InjectionToken with a custom class that does it (possible? recommended?)
  2. Ignore the missing type check 😦

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
crisbetocommented, Nov 26, 2022

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.

0reactions
Harpushcommented, Nov 26, 2022

@crisbeto ok… Wasn’t 15 a major release? 🤔

Read more comments on GitHub >

github_iconTop 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 >

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