Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duck types do not handle null or undefined #82

Closed
slifty opened this issue Aug 24, 2020 · 2 comments
Closed

Duck types do not handle null or undefined #82

slifty opened this issue Aug 24, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@slifty
Copy link
Member

slifty commented Aug 24, 2020

Bug

Current Behavior

We introduced some duck typing for IAppliance, Payload, and PayloadArray.

The issue is that if you try to duck type a null or undefined value, it breaks with the following error:

/Users/slifty/Dropbox/Code/NodeJS/tvkitchen/base/packages/interfaces/lib/IAppliance.js:86
_defineProperty(IAppliance, "isIAppliance", obj => IAppliance.duckTypeProperties.every(property => Object.prototype.hasOwnProperty.call(obj, property)));
                                                                                                                                   ^

TypeError: Cannot convert undefined or null to object
    at hasOwnProperty (<anonymous>)
    at /Users/slifty/Dropbox/Code/NodeJS/tvkitchen/base/packages/interfaces/lib/IAppliance.js:86:132
    at Array.every (<anonymous>)
    at Function.isIAppliance (/Users/slifty/Dropbox/Code/NodeJS/tvkitchen/base/packages/interfaces/lib/IAppliance.js:86:82)
    at Object.<anonymous> (/Users/slifty/Dropbox/Code/NodeJS/tvkitchen/countertop/src/scripts/_sandbox.js:15:12)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Module._compile (/Users/slifty/Dropbox/Code/NodeJS/tvkitchen/countertop/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Object.newLoader [as .js] (/Users/slifty/Dropbox/Code/NodeJS/tvkitchen/countertop/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:996:32)

Input

Code, command line, or other relevant input / steps to reproduce the issue.

import { IAppliance } from '@tvkitchen/base-interfaces'

IAppliance.isIAppliance(null)

Expected Behavior

It should return true or false as documented, since null and undefined values are either IAppliances or they are not.

undefined should evoke a false value.

null should probably evoke a false value, though it is a bit of an existential question: Is null an instance of everything, or an instance of nothing?

Possible Solutions

We should update duck type instances to detect null and undefined and return a proper value.

Related Issues

A list of related issues

@slifty slifty added the bug Something isn't working label Aug 24, 2020
@slifty
Copy link
Member Author

slifty commented Aug 24, 2020

Update: null is an instance of nothing:

const x = null
x instanceof Object // false

@slifty
Copy link
Member Author

slifty commented Mar 4, 2022

I think this issue is going to be obsolete when we move to TypeScript (#114)

As a result, I'm closing the issue.

@slifty slifty closed this as completed Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant