How to declare a type in TypeScript that only incl

2019-08-05 10:59发布

问题:

Is it in TypeScript somehow possible to define a type so that it only includes objects but not functions?

Example:

type T = { [name: string]: any } // How to modify this to only accepts objects???

const t: T = () => {} // <- This should not work after modification

Thanks for your help.

回答1:

There's no perfect way to refer to a type like "an object which is not a function", since that would require true subtraction types, and that doesn't exist in TypeScript (as of 3.1 at least).

One easy-enough workaround is to look at the Function interface and describe something which is definitely not a Function, but which would match most non-function objects you're likely to run into. Example:

type NotAFunction = { [k: string]: unknown } & ({ bind?: never } | { call?: never });

That means "an object with some unspecified keys, which is either missing a bind property or a call property". Let's see how it behaves:

const okayObject: NotAFunction = { a: "hey", b: 2, c: true };
const notOkayObject: NotAFunction = () => {}; // error, call is not undefined

Good.

The reason that this is a workaround and not a straightforward solution is that some non-functions might have both a call and a bind property, just by coincidence, and you'll get an undesirable error:

const surprisinglyNotOkay: NotAFunction = { 
  publication: "magazine", 
  bind: "staples",
  call: "867-5309",
  fax: null
}; // error, call is not undefined

If you really need to support such objects you can change NotAFunction to be more complicated and exclude fewer non-functions, but there will likely always be something the definition gets wrong. It's up to you how far you want to go.

Hope that helps. Good luck!