gkz/type-check

Allow for literal `typeof` comparison

dead-claudia opened this issue · 4 comments

I recently wrote a type assertion library/framework based on this for a LiveScript project, and in the conversion, I've ran into the following problem: I would like to check against less-specific typeof values, the basic 7:

  • boolean
  • number
  • object
  • string
  • function
  • symbol
  • undefined

Could this be added? I'd rather not have to resort to the below hack, as it would be rather slow:

for type in <[boolean number object string function symbol undefined]>
    customTypes[type] = typeOf: '*', validate: -> typeof it == type

+1

having function would be really nice for sure, I'll try to carve out some time to submit a pull request for it

gkz commented

I don't understand, why don't you simply use the predefined types "Boolean", "Number", etc.
Can you expand your use case?

@gkz For example, I may want exclusively primitive numbers for a performance-sensitive math library, or I want to accept any primitive object (including null). Also, if I just want something that I can call (generator, function, etc. works), typeof! (->*) == 'Generator', which isn't particularly helpful.

Or in my particular case, I was dealing with a large amount of structural typing (duck typing, as opposed to nominal), and I wanted to support it. The best way to address that is with objects (with a not-null + normal typeof assertion as a base).

gkz commented

So, I could make the type-of property for a custom type optional, and you could implement whatever logic you want in the validate function?