I am trying to find proper signature (current version of TypeScript is 1.7) for function that should accept only reference types, not primitives:
function onlyObject(x: ???) {
if (typeof x !== 'object') {
throw "bad arg!";
}
}
So for function above this should work:
onlyObject({ });
onlyObject(new Date());
onlyObject(new Number(1));
onlyObject(null);
onlyObject(function () { });
but this will fail in compile time:
onlyObject("awd");
onlyObject(1);
onlyObject(false);
typeof x === 'object'is true. Regarding differentiation betweenstringandString- TypeScript does a bit, you can check it here: bit.ly/1ZBweTs