【问题标题】:Why can Record<string, any> equal Functions?为什么 Record<string, any> 可以等于函数?
【发布时间】:2022-01-20 14:23:59
【问题描述】:

我一直在测试各种类型的打字稿,直到遇到以下情况。

为什么Record&lt;string, any&gt; 可以等于函数?

type C = { [key: string]: any } // Record<string, any>;
const C0: C = undefined // error
const C1: C = null // error
const C2: C = 2 // error
const C3: C = 'hello world' // error
const C4: C = { foo: 'bar' } // ok
const C5: C = () => undefined // ok

但是Records&lt;string, unknown&gt; 不能?

type B = { [key: string]: unknown } // Record<string, unknown>;
const B0: B = undefined // error
const B1: B = null // error
const B2: B = 2 // error
const B3: B = 'hello world' // error
const B4: B = { foo: 'bar' } // ok
const B5: B = () => undefined // error

Typescript playground

【问题讨论】:

    标签: javascript typescript function object


    【解决方案1】:

    Record&lt;string, any&gt; 是可分配性的特殊情况,它基本上意味着任何对象类型。这在this GitHub issue中有解释

    如果目标类型有,通常源类型必须有一个索引签名,但是对于 :any 这实际上没有任何暗示(根据定义,每个属性都必须匹配 any),所以我们将 [s: string]: any a出于可分配性的原因,无操作。这启用了一些以前不起作用的模式:

    function doSomething(obj: { [s: string]: any}) {
      // ...
    }
    
    const obj = { a: 1, b: 2 };
    // WAS ERROR: 'obj' lacks index signature
    doSomething(obj);
    

    这在某些情况下会产生一些不良的可分配性,因此我们没有将此规则应用于 : unknown 以便您可以选择您想要的行为。

    【讨论】:

    • 我不明白。你是说Function 是对象吗?还是你暗示Record&lt;string, any&gt; = any
    • @SILENT 是的,函数是对象。所有函数签名都扩展 Function 扩展 Object typescriptlang.org/play#code/…
    猜你喜欢
    • 2021-12-13
    • 2021-03-13
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多