Forcing specification of a generic Closure type

Generally, if you fail to specify a generic type parameter in Closure, it won't raise an error. This is different than many other languages, including TypeScript. Closure will treat the type as an "unknown" type, which are usually ignored. (It's possible to set compiler flags to have it complain about unknown types, but these can only be set globally and often too noisy to really use.)

I have a Closure class, Response<T>. I would like all Response instances to specify a type for <T>, not simply leave it untyped. To do that, I'd like to force a compile-time error whenever you try to instantiate a generic instance, so I can find and fix all such instances.

I've been trying to coerce this behaviour out of the Closure Type Transformation Language, but nothing I try is actually creating an error. Here's my latest attempt, but it seems to be ignored:

/**
 * @template OPT_RESPONSE_TYPE
 * @template RESPONSE_TYPE := cond(
 *     !isUnknown(OPT_RESPONSE_TYPE),
 *     OPT_RESPONSE_TYPE,
 *     printType(
 *         'ERROR: Please specify a non-generic type for Response',
 *         typeExpr('ERROR')
 *     )
 * ) =:
 *
 * @param {RESPONSE_TYPE=} value
 */
const Response = class {
  constructor(value = undefined) {
    /** @const */
    this.value = value;
  }
}

As a half-measure, I've been converting any unknown/unspecified generic types to the undefined type, because that will encourage the compiler to raise more errors for potentially-unsafe use:

 * @template OPT_RESPONSE_TYPE
 * @template RESPONSE_TYPE := cond(isUnknown(OPT_RESPONSE_TYPE), 'undefined', OPT_RESPONSE_TYPE) =:

Is there any more direct way to require specification of a generic type parameter in Closure?

Upvotes: 0

Views: 257

Answers (1)

Chad Killingsworth
Chad Killingsworth

Reputation: 14411

This is probably something you could do with a custom conformance config. If you are brave enough to delve into the undocumented type transformation language, that shouldn't be a problem either.

Upvotes: 1

Related Questions