Could somebody explain me which is the difference between {}
and any
?
for example, which's the difference between setting generic interface parameter as Interface<{}>
or as Interface<any>
.
Any ideas?
Could somebody explain me which is the difference between {}
and any
?
for example, which's the difference between setting generic interface parameter as Interface<{}>
or as Interface<any>
.
Any ideas?
to better understand what does {}
mean, check https://blog.mariusschulz.com/2017/02/24/typescript-2-2-the-object-type
{}
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'}
Although null | undefined
is not allowed
// Expect errors
const test6: {} = null
const test7: {} = undefined
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
So there are types in JavaScript. Like number
, string
, object
and boolean
. any
matches any of these types. {}
just matches an empty object
.
function fn1(foo: any): void {}
function fn2(foo: {}): void {}
fn1(23); // OK
fn1('test'); // OK
fn1(undefined); // OK
fn2({}); // OK
fn2(23); // Not OK
fn2(null); // Not OK