简体   繁体   English

如何在Closure中为函数调用指定泛型类型参数?

[英]How do I specify generic type parameters for a function invocation in Closure?

I am working on reducing the use of accidentally-unknown generic type parameters in a code base. 我正在努力减少在代码库中使用意外未知的泛型类型参数。 I am using a Closure TTL incantation like the following to coerce unknown generic types to the undefined type, hopefully provoking errors that let me find these cases (see this related question ). 我使用像下面这样的Closure TTL咒语将未知的泛型类型强制转换为undefined类型,希望能够引发错误,让我找到这些案例(请参阅此相关问题 )。

 * @template T
 * @template TYPE_I_ACTUALLY_USE := cond(isUnknown(T), 'undefined', T) =:

That has brought me to a case like this: 这让我想到了这样一个案例:

/**
 * @template T
 * @template R := cond(isUnknown(T), 'undefined', T) =: 
 * @param {function():R} factory
 * @return {{prop: R}}
 */
function factoryWrapper(factory) {
  return {prop: factory()};
}

var instance = factoryWrapper(() => 2);

This factoryWrapper function takes a factory function that produces a value of type R , calls it, and wraps the result in {prop: ...} , for a return type of {prop: R} . 这个factoryWrapper函数接受一个factory函数,它产生一个R类型的值,调用它,并将结果包装在{prop: ...} ,返回类型为{prop: R} But because I don't specify T or R when calling factoryWrapper , it doesn't know what type they're supposed to be, and can't tell whether the provided factory function returns the right type, so it produces an error or untyped output: 但是因为我在调用factoryWrapper时没有指定TR ,它不知道它们应该是什么类型,并且无法判断提供的工厂函数是否返回正确的类型,因此它会产生错误或无类型输出:

input0:11: WARNING - inconsistent return type
found   : number
required: undefined
var instance = factoryWrapper(() => 2);

In a language with a native type annotation syntax, like TypeScript, I might fix this by including the generic type parameter in the invocation: 在具有本机类型注释语法的语言(如TypeScript)中,我可以通过在调用中包含泛型类型参数来解决此问题:

var instance = factoryWrapper<number>(() => 2);

However, I haven't been able to figure out any equivalent syntax for Closure. 但是,我还没有找到Closure的任何等效语法。 I took a few guesses, like the following, but none worked. 我做了一些猜测,如下所示,但都没有奏效。

var instance = factoryWrapper/** <number> */(() => 2);

How do I specify generic type parameters for a function invocation in Closure JavaScript? 如何在Closure JavaScript中为函数调用指定泛型类型参数?

Closure Compiler doesn't allow you to specify type parameters when calling a generic function. Closure Compiler不允许您在调用泛型函数时指定类型参数。 But I think casting the callback will do what you want. 但我认为投射回调会做你想要的。 Something like: var instance = factoryWrapper(/** @type {function(): number} */ (() => 2)); 类似于: var instance = factoryWrapper(/** @type {function(): number} */ (() => 2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Google Closure编译器中为对象类型指定@param @return? - How do I specify @param @return for object type in Google Closure Compiler? 如何在TypeScript中的rest参数之后指定回调函数? - How do I specify a callback function after rest parameters in TypeScript? 如何在JetBrains IDE中为闭包指定“this”值的类型? - How can I specify the type of the “this” value for a closure in JetBrains IDEs? 我需要在Javascript闭包中指定参数吗 - Do I need to specify parameter in Javascript closure 如何使用sinon监视JavaScript中的导出函数调用 - How do I spy an exported function invocation in JavaScript using sinon 如何在 Google Closure Compiler 中指定 ES6 导入的路径? - How do I specify the path for ES6 import in Google Closure Compiler? 如何使用Twitter Bootstrap指定多个javascript模态参数? - How do I specify multiple javascript modal parameters with Twitter Bootstrap? 我是否需要在 javascript/typescript 中为匿名函数指定返回类型? - Do I need to specify a return type for an anonymous function in javascript / typescript? 为什么我必须在 TS 函数类型中指定参数名称? - Why do I have to specify parameter names in a TS function type? 如何防止闭包保留javascript函数的参数? - How do I prevent an argument to a javascript function being retained by a closure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM