问题描述
谁能解释一下 {}
和 any
之间的区别是什么?
Could somebody explain me which is the difference between {}
and any
?
例如,这是将通用接口参数设置为 Interface
或设置为 Interface
之间的区别.
for example, which's the difference between setting generic interface parameter as Interface<{}>
or as Interface<any>
.
有什么想法吗?
推荐答案
要更好地理解 {}
的含义,请查看 https://blog.mariusschulz.com/2017/02/24/typescript-2-2-the-object-输入
to better understand what does {}
mean, check https://blog.mariusschulz.com/2017/02/24/typescript-2-2-the-object-type
{}
是顶级类型.这是什么意思 ?如果您将某些内容注释为 {}
,它可以是以下任何类型:string |数量 |布尔值 |对象 |{[key:string]: 任意} |对象 |任何[]
{}
is a top type. What does it mean ? if you annotate something as {}
it can be any of following types: string | number | boolean | object | {[key:string]: any} | Object | any[]
const test1: {} = 'hello'
const test2: {} = 123
const test3: {} = false
const test4: {} = {foo:'bar'}
虽然 null |未定义
是不允许的
// Expect errors
const test6: {} = null
const test7: {} = undefined
any
完全关闭类型检查器,你可以做任何疯狂"的事情,你已经习惯了香草 JS.专业提示:不要在家里使用它 :) 一定要在 ts-lint
any
completely turns off type checker and you can do anything "crazy", that you've been used to from vanilla JS. PRO TIP: don't use this at home :) definitely use no-any
rule within ts-lint
这篇关于打字稿空对象和任何区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!