【发布时间】:2022-01-20 14:23:59
【问题描述】:
我一直在测试各种类型的打字稿,直到遇到以下情况。
为什么Record<string, any> 可以等于函数?
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<string, unknown> 不能?
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
【问题讨论】:
标签: javascript typescript function object