How to declare a type in TypeScript that only includes objects and not functions

放肆的年华 提交于 2019-12-24 06:38:54

问题


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!



来源:https://stackoverflow.com/questions/52692606/how-to-declare-a-type-in-typescript-that-only-includes-objects-and-not-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!