codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Support typed object #90

Closed Riim closed 8 years ago

Riim commented 8 years ago
function on(target: EventTarget, handlers: { [key: string]: Function }) {/* ... */}

Created check:

        if (!(types != null && (typeof types === 'undefined' ? 'undefined' : _typeof(types)) === 'object')) {
            throw new TypeError('Value of argument "types" violates contract, expected string | { [key: string]: Function\n} got ' + (types === null ? 'null' : (typeof types === 'undefined' ? 'undefined' : _typeof(types)) === 'object' && types.constructor ? types.constructor.name || '[Unknown Object]' : typeof types === 'undefined' ? 'undefined' : _typeof(types)));
        }

Checked what types is the object, but no type checking of values in the object.

phpnode commented 8 years ago

hmm the check you posted doesn't seem to match the function signature?

I guess you're looking for something like this?

if (!(handlers != null && typeof handlers === 'object' && Object.keys(handlers).every(key => typeof handlers[key] === 'function')) {
  // TypeError
}
Riim commented 8 years ago

the check you posted doesn't seem to match the function signature?

Oops, full example:

const EventManager = {
    on(
        obj: EventTarget,
        types: string|{ [key: string]: Function },
        fn?: Function|Object,
        context?: Object
    ): Object {
        if (typeof types === 'object') {
            for (const type in types) {
                EventManager._on(obj, type, types[type], fn);
            }
        } else {
            types = Util.splitWords(types);

            for (let i = 0; i < types.length; i++) {
                EventManager._on(obj, types[i], fn, context);
            }
        }

        return EventManager;
    },

Out:

var EventManager = {
    on: function on(obj, types, fn, context) {
        if (!(obj instanceof EventTarget)) {
            throw new TypeError('Value of argument "obj" violates contract, expected EventTarget got ' + (obj === null ? 'null' : (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && obj.constructor ? obj.constructor.name || '[Unknown Object]' : typeof obj === 'undefined' ? 'undefined' : _typeof(obj)));
        }

        if (!(typeof types === 'string' || types != null && (typeof types === 'undefined' ? 'undefined' : _typeof(types)) === 'object')) {
            throw new TypeError('Value of argument "types" violates contract, expected string | { [key: string]: Function\n} got ' + (types === null ? 'null' : (typeof types === 'undefined' ? 'undefined' : _typeof(types)) === 'object' && types.constructor ? types.constructor.name || '[Unknown Object]' : typeof types === 'undefined' ? 'undefined' : _typeof(types)));
        }

        if (!(fn === undefined || typeof fn === 'function' || fn instanceof Object)) {
            throw new TypeError('Value of optional argument "fn" violates contract, expected Function | Object got ' + (fn === null ? 'null' : (typeof fn === 'undefined' ? 'undefined' : _typeof(fn)) === 'object' && fn.constructor ? fn.constructor.name || '[Unknown Object]' : typeof fn === 'undefined' ? 'undefined' : _typeof(fn)));
        }

        if (!(context === undefined || context instanceof Object)) {
            throw new TypeError('Value of optional argument "context" violates contract, expected Object got ' + (context === null ? 'null' : (typeof context === 'undefined' ? 'undefined' : _typeof(context)) === 'object' && context.constructor ? context.constructor.name || '[Unknown Object]' : typeof context === 'undefined' ? 'undefined' : _typeof(context)));
        }

        if ((typeof types === 'undefined' ? 'undefined' : _typeof(types)) === 'object') {
            for (var type in types) {
                EventManager._on(obj, type, types[type], fn);
            }
        } else {
            types = Util.splitWords(types);

            for (var i = 0; i < types.length; i++) {
                EventManager._on(obj, types[i], fn, context);
            }
        }

        return EventManager;
    },

I guess you're looking for something like this?

This is what we need.

phpnode commented 8 years ago

fixed in 3.5.1