【问题标题】:How to specify type of function who's parameter object has a key and allows any other keys?如何指定参数对象具有键并允许任何其他键的函数类型?
【发布时间】:2020-03-23 00:42:04
【问题描述】:

使用 typescript,我们可以为需要有键的对象定义一个接口,并且可以额外允许任何其他键:

interface ObjectWithTrace {
  trace: string;
  [index: string]: any
}
const traced: ObjectWithTrace = { trace: 'x', foo: 'bar' }; // looks good
const untraced: ObjectWithTrace = { foo: 'bar' }; // Error: Property 'trace' is missing in type '{ foo: string; }' but required in type 'ObjectWithTrace'. ts(2741)   

在上面的示例中,trace 是必需的密钥。我们可以向对象添加我们想要的任何键,只要定义了trace 键,打字稿就很高兴。完美

现在,当尝试扩展此逻辑以应用于函数的参数时,会发生错误:

type FunctionWithParamWithTrace = (args: {
  trace: string;
  [index: string]: any
}) => any;
const doSomethingAndTrace: FunctionWithParamWithTrace = (args: { trace: string }) => {}; // looks good
const doSomethingElseAndTrace: FunctionWithParamWithTrace = (args: { trace: string, foo: 'bar' }) => {} /*
 Error: Type '(args: { trace: string; foo: "bar"; }) => void' is not assignable to type 'FunctionWithParamWithTrace'.
  Types of parameters 'args' and 'args' are incompatible.
    Property 'foo' is missing in type '{ [index: string]: any; trace: string; }' but required in type '{ trace: string; foo: "bar"; }'.ts(2322)
*/

我好像错过了什么。有没有办法为一个函数定义一个类型,该函数期望只有 on 参数 - 并让该参数允许任何具有任何值的键,同时还需要一个键具体存在(例如,一个名为 trace 的属性)?

我希望支持的是:

const doSomethingWithTrace: FunctionWithParamWithTrace = (args: { trace: string, foo: string }) => {}; // looks good
const doSomethingWithoutTrace: FunctionWithParamWithTrace = (args: { foo: string }) => {} // Error: Property 'trace' is missing in type '{ foo: string; }'...

【问题讨论】:

    标签: typescript typescript-generics


    【解决方案1】:

    您需要的是能够通过子类型扩展您想要的类型。这可以通过引入泛型参数来实现:

    type FunctionWithParamWithTrace<Args extends {
      trace: string;
      [index: string]: any
    }> = (args: Args) => any;
    
    const doSomethingAndTrace: FunctionWithParamWithTrace<{ trace: string }> 
    = (args) => {}; // looks good
    const doSomethingElseAndTrace: FunctionWithParamWithTrace<{ trace: string, foo: 'bar' }> 
    = (args) => {} // works
    

    您不能使用更严格的类型作为参数的原因在下面的代码中可见:

    type FunctionWithParamWithTrace = (args: {
      trace: string;
      [index: string]: any
    }) => any;
    
    type FunctionWithMoreStrictParameters = (args: { trace: string, foo: string }) => any
    
    type NoItDoesNotExtends 
    = FunctionWithMoreStrictParameters extends FunctionWithParamWithTrace 
    ? true 
    : false // false
    
    

    这意味着您更严格的功能不是原始功能的子集,因此不能用作它。这是为什么?想象一下你有一个函数可以使用更精确的参数,比如总是得到foo的函数,这个函数不能用来替换可以接受任何对象的函数。这就是无法执行此操作的原因。


    下面的一些其他示例可能会更多地说明为什么使用更严格的参数函数不是一个好主意,而 TS 会阻止这样:

    function f(arg: {a: string, b: string | number}) {
        return arg.b.concat('b cannot be used as string as it can be number') // error
    }
    
    function g(arg: {a: string, b: string}) {
        return arg.b.concat(' b can be used as string') // pass compilation
    }
    

    函数g参数类型是函数f参数类型的子集,因此应该可以代替f使用?完全相反,可以使用函数f 代替g,因为它能够使用更广泛的类型,这意味着它的主体需要执行额外的检查。这在函数f 中可见,我们不能只使用string | number 作为string,我们需要对值执行额外的检查。但是当我们查看g 时,它不需要进行检查,因为它仅适用于string。如果我们使用g 而不是f 会发生什么,我们会得到number 参数,这当然是运行时崩溃。

    总的来说,函数类型是另一个函数类型的子集,如果它在参数类型上更灵活,而不是更严格。

    【讨论】:

    • 是否可以在不每次都明确定义泛型参数的情况下做到这一点?即,像通常那样定义函数,并且知道如果它作为参数传递给另一个需要类型 FunctionWithParamWithTrace 的函数,它将满足类型标准
    • 问题是这样的函数不应该作为FunctionWithParamWithTrace 传递,因为它会破坏运行时。检查我添加到答案中的内容
    • 感谢您的详尽解释 - 这很有意义
    猜你喜欢
    • 2018-09-28
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多