Skip to content

FR: Add generic type <T> to Type or suggest alternative #30115

Closed
@matanlurey

Description

@matanlurey

Apologies and please close if this a duplicate, but I couldn't find it.

In AngularDart, Type is used both as a compile-time symbol/token for many operations, such as dependency injection. For example, here is a way a user might say "allow injecting RpcService, and create a new instance of CachedRpcService to satisfy the dependency":

const Provider(RpcService, useClass: CachedRpcService)

Unfortunately, there is no good way for me to use types to help users from making silly mistakes. For example, this is totally valid, and will appear to work until runtime:

const Provider(RpcService, useClass: LinkedHashMap)

I realize we could write our own lints+analyzer plugin+yada yada, but this seems like a legitimate place where a generic type argument to Type could give this to us for free. For example, imagine we did the following:

abstract class Provider<T> {
  const Provider({Type<T> useClass}); 
}

const Provider<RpcService>(useClass: CachedRpcService) // OK
const Provider<RpcService>(useClass: LinkedHashMap) // Static analysis errors

I considered the alternative of constructor tear-offs, for example:

const Provider<RpcService>(useFactory: CachedRpcService.new)

But I run into a similar issue, there is no way to statically say "a function with any number or type of arguments as long as it returns T". Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions